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 <[EMAIL PROTECTED]> wrote:
On 3/2/07, Jakob Buchgraber <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
URL: http://blog.bisna.com
São Carlos - SP/Brazil

Reply via email to