FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » const var, self:: or static::
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
const var, self:: or static:: [message #177963] Wed, 02 May 2012 09:45 Go to next message
Simon is currently offline  Simon
Messages: 29
Registered: February 2011
Karma: 0
Junior Member
If I have something like...

//
class MyClass
{
const MY_CONST = 10;

...
}
//

Inside the class, is it 'better' to use self::MY_CONST or static::MY_CONST?

I say 'better' because it works in both cases.

Or is it just a personal choice.

Thanks

Simon
Re: const var, self:: or static:: [message #177964 is a reply to message #177963] Wed, 02 May 2012 11:13 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/2/2012 5:45 AM, Simon wrote:
> If I have something like...
>
> //
> class MyClass
> {
> const MY_CONST = 10;
>
> ...
> }
> //
>
> Inside the class, is it 'better' to use self::MY_CONST or static::MY_CONST?
>
> I say 'better' because it works in both cases.
>
> Or is it just a personal choice.
>
> Thanks
>
> Simon

There is no "better" - it depends on what you want. There is no
difference when you have a single class like this, but there can be when
you have a derived class. self:: will always call a static function in
the base class, while static:: will call a static function in the
derived class (I've never tried it with constants, but the same may be
true).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: const var, self:: or static:: [message #177965 is a reply to message #177963] Wed, 02 May 2012 15:25 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 02.05.2012 11:45, schrieb Simon:
> If I have something like...
>
> //
> class MyClass
> {
> const MY_CONST = 10;
>
> ...
> }
> //
>
> Inside the class, is it 'better' to use self::MY_CONST or static::MY_CONST?
>
> I say 'better' because it works in both cases.
>
> Or is it just a personal choice.

It works with static:: but it is not documented. Maybe a PHP4 compatibility thing.

I would not use it. Documented are self::$var parent::$var and Classname::$var

/Str.
Re: const var, self:: or static:: [message #177966 is a reply to message #177965] Wed, 02 May 2012 17:55 Go to previous message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(M. Strobel)

> Am 02.05.2012 11:45, schrieb Simon:
>> If I have something like...
>>
>> //
>> class MyClass
>> {
>> const MY_CONST = 10;
>>
>> ...
>> }
>> //
>>
>> Inside the class, is it 'better' to use self::MY_CONST or static::MY_CONST?
>>
>> I say 'better' because it works in both cases.
>>
>> Or is it just a personal choice.
>
> It works with static:: but it is not documented. Maybe a PHP4 compatibility thing.

'static' is a new PHP 5.3 thing called Late Static Binding:

http://www.php.net/manual/en/language.oop5.late-static-bindings.php

The behaviour on constants is the same as on member variables and
methods. But see also

http://blogs.neoseeker.com/tekmosis/5576-late-static-binding-of-variables-c onstants-in-php-5-3/

Micha

--
http://mfesser.de/blickwinkel
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: installing PHP zip onto a solaris system
Next Topic: Session variables..
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 01:38:05 GMT 2024

Total time taken to generate the page: 0.01988 seconds