Constant Parameters

php.internals

Jakob Buchgraber

19 years ago
Hello! It would be nice if in PHP 6 using constant parameters for functions and methods would be possible. This feature is e.g. available in Java. function doSth (const A) { if (defined ("A")) echo "A is a constant"; } doSth ("foo"); Cheers, Jay

Brian Moon

19 years ago
Jakob Buchgraber wrote:
> Hello! > > It would be nice if in PHP 6 using constant parameters for functions and > methods would be possible. This feature is e.g. available in Java. > > function doSth (const A) { > if (defined ("A")) echo "A is a constant"; > } > > doSth ("foo"); > > Cheers, > Jay >
Hmm, can you give me a better example of how that would be used? I don't see the use in this short example.
-- Brian Moon ------------- http://dealnews.com/ It's good to be cheap =)

Marco Kaiser

19 years ago
Hi Jakob,
> It would be nice if in PHP 6 using constant parameters for functions > and methods would be possible. This feature is e.g. available in Java. > > function doSth (const A) { > if (defined ("A")) echo "A is a constant"; > } > > doSth ("foo"); > > Cheers, > Jay >
This makes absolute no sense, why you want this? -- Marco

Jakob Buchgraber

19 years ago
Marco Kaiser wrote:
> Hi Jakob, >> It would be nice if in PHP 6 using constant parameters for functions >> and methods would be possible. This feature is e.g. available in Java. >> >> function doSth (const A) { >> if (defined ("A")) echo "A is a constant"; >> } >> >> doSth ("foo"); >> >> Cheers, >> Jay >> > This makes absolute no sense, why you want this? > > -- Marco
It's just a way of ensuring that a parameter cannot be changed within a method (e.g. an id). I am using this feature frequently in Java. It actually got no "deeper" sense, but it's just a nice feature I'd like to use in PHP too. :-) Cheers, Jay

Pierre Joye

19 years ago
On 3/2/07, Jakob Buchgraber <jakob.buchgraber@gmail.com> wrote:
> Marco Kaiser wrote: > > Hi Jakob, > >> It would be nice if in PHP 6 using constant parameters for functions > >> and methods would be possible. This feature is e.g. available in Java. > >> > >> function doSth (const A) { > >> if (defined ("A")) echo "A is a constant"; > >> } > >> > >> doSth ("foo"); > >> > >> Cheers, > >> Jay > >> > > This makes absolute no sense, why you want this? > > > > -- Marco > > It's just a way of ensuring that a parameter cannot be changed within a > method (e.g. an id). I am using this feature frequently in Java. It > actually got no "deeper" sense, but it's just a nice feature I'd like to > use in PHP too. :-)
It actually comes from the C/C++ "const" like foo(const char *str). It makes very little sense to have it in PHP. --Pierre

Guilherme Blanco

19 years ago
Hi, I think it's a great possibility too, specially when dealing with 3rd party components. If you have an interface in your application to be implemented by others, you can notice them about the constant argument (and should NOT be redefined) and throw a fatal error (altho I still do not understand why the notice, warning and fatal errors are not throwable). Notice that I am mentioning this since PHP5 treats object arguments as references. So, a modification of a property in a const argument MUST be impossible. If PHP keeps the arguments as by-value, I would disagree with Jakob's suggestion; but, since you can modify a referenced object, this feature should exist. I am not able to imagine a functional example, but you can imagine a situation of a Database argument being sent in a function: $db = new DBConnection($properties); // Notice, still not connected to DB $third_party_obj->foo($db); $db->connect(); Now, imagine $third_party_obj is an instance of 3rd-party implemented feature... something like: class MyCustomObj implements Extandable3rdParty { //... } Ok... the idea is finished here... using a const argument, this should not be possible: public function foo($db) { $db->setDatabase("anotherDB"); //.... } Why? Because $db should be a constant and could not be modified. Allowing this, the 3rd party component could redefine some important properties inside of your object and may cause unbelievable issues in your appplication. My example illustrates a possible thing (a database name change) without the use of constants. If this feature exists, a fatal error is displayed pointing to the setDatabase line... a possible message: "You cannot modify a constant argument [0]" Maybe now you agree with his idea. Best regards, On 3/2/07, Pierre <pierre.php@gmail.com> wrote: > On 3/2/07, Jakob Buchgraber <jakob.buchgraber@gmail.com> wrote: > > Marco Kaiser wrote: > > > Hi Jakob, > > >> It would be nice if in PHP 6 using constant parameters for functions > > >> and methods would be possible. This feature is e.g. available in Java. > > >> > > >> function doSth (const A) { > > >> if (defined ("A")) echo "A is a constant"; > > >> } > > >> > > >> doSth ("foo"); > > >> > > >> Cheers, > > >> Jay > > >> > > > This makes absolute no sense, why you want this? > > > > > > -- Marco > > > > It's just a way of ensuring that a parameter cannot be changed within a > > method (e.g. an id). I am using this feature frequently in Java. It > > actually got no "deeper" sense, but it's just a nice feature I'd like to > > use in PHP too. :-) > > It actually comes from the C/C++ "const" like foo(const char *str). It > makes very little sense to have it in PHP. > > --Pierre > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Guilherme Blanco - Web Developer CBC - Certified Bindows Consultant Cell Phone: +55 (16) 9166-6902 MSN: guilhermeblanco@hotmail.com URL: http://blog.bisna.com São Carlos - SP/Brazil

Sean Coates

19 years ago
> $db = new DBConnection($properties); > // Notice, still not connected to DB > $third_party_obj->foo($db); > $db->connect();
In the requested feature, the "protection" is in the wrong hands (that of the third party object). You're much better off changing your call to: $third_party_obj->foo(clone $db); S

Brian Moon

19 years ago
Guilherme Blanco wrote:
> Why? Because $db should be a constant and could not be modified. > Allowing this, the 3rd party component could redefine some important > properties inside of your object and may cause unbelievable issues in > your appplication.
This makes it sound like you let other people write code that you then include in your applications on your site. Is that right? Otherwise, don't you (or your team) write the code that goes on your site? On top of that, this 3rd party will be making the function. So, they control the definition, not you. You can call time pass as const. So, this is not a solution to keep 3rd parties from breaking your stuff. It makes no sense to me, but you can never tell what OOP person may jump in here and think it is the best thing since left handed backwards inheritance or something. =)
-- Brian Moon ------------- http://dealnews.com/ It's good to be cheap =)

Brian Moon

19 years ago
Brian Moon wrote:
> You can call time pass as const.
Damn, need to proof read. Should be "CAN'T"
-- Brian Moon ------------- http://dealnews.com/ It's good to be cheap =)