Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5802 > unrolled thread

const var, self:: or static::

Started bySimon <bad@example.com>
First post2012-05-02 11:45 +0200
Last post2012-05-02 19:55 +0200
Articles 4 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  const var, self:: or static:: Simon <bad@example.com> - 2012-05-02 11:45 +0200
    Re: const var, self:: or static:: Jerry Stuckle <jstucklex@attglobal.net> - 2012-05-02 07:13 -0400
    Re: const var, self:: or static:: "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-05-02 17:25 +0200
      Re: const var, self:: or static:: Michael Fesser <netizen@gmx.de> - 2012-05-02 19:55 +0200

#5802 — const var, self:: or static::

FromSimon <bad@example.com>
Date2012-05-02 11:45 +0200
Subjectconst var, self:: or static::
Message-ID<a0ce19Fb48U1@mid.individual.net>
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

[toc] | [next] | [standalone]


#5803

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-05-02 07:13 -0400
Message-ID<jnr4sp$78d$1@dont-email.me>
In reply to#5802
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#5804

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-05-02 17:25 +0200
Message-ID<a0d1vjF3qmU1@mid.uni-berlin.de>
In reply to#5802
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.

[toc] | [prev] | [next] | [standalone]


#5805

FromMichael Fesser <netizen@gmx.de>
Date2012-05-02 19:55 +0200
Message-ID<lqs2q7128hi5sq5k7gudbnta9t7a26qr6l@mfesser.de>
In reply to#5804
.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-constants-in-php-5-3/

Micha

-- 
http://mfesser.de/blickwinkel

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web