53

I saw this example from php.net:

<?php
class MyClass {

     const MY_CONST = "yonder";

     public function __construct() {

          $c = get_class( $this );
          echo $c::MY_CONST;
     }
}

class ChildClass extends MyClass {

     const MY_CONST = "bar";
}

$x = new ChildClass(); // prints 'bar'
$y = new MyClass(); // prints 'yonder'
?>

But $c::MY_CONST is only recognized in version 5.3.0 or later. The class I'm writing may be distributed a lot.

Basically, I have defined a constant in ChildClass and one of the functions in MyClass (father class) needs to use the constant. Any idea?

5 Answers 5

122

How about using static::MY_CONST?

7
  • 3
    I do not get why people are getting to deep into OOP in other answers. Your solution is the only correct one and way simpler Commented Jan 15, 2014 at 10:26
  • 2
    There is something wrong about accessing a const by using the static keyword. Could you explain why that works? PHP Docs confused me in this as well. Thanks.
    – pavlindrom
    Commented Jul 2, 2014 at 15:26
  • 4
    @checksum: no, thats wrong – self::MY_CONST prints in both cases "yonder" – constant defined in child. Question was "How to access constant defined in child class from parent class functions?".
    – pevik
    Commented Feb 22, 2015 at 21:16
  • 1
    I'm super glad this works but I'm left scratching my head...why would it? Eh whatever my stuff works now. Thanks poster! Commented Jun 10, 2015 at 19:25
  • 4
    Note about the implementation: self::MY_CONST is based on Late static bindings, released in PHP 5.3.0 (implemented in git Added support for Late Static Binding. (Dmitry, Etienne Kneuss)). Core implementation is in get_called_class(). PHP <= 5.2.x gives error Parse error: syntax error, unexpected T_STATIC.
    – pevik
    Commented Dec 8, 2015 at 20:06
30

Since php 5.3:

Use static::MY_CONST


More details on static

In this case the keyword static is a reference to the actually called class.

This illustrates the difference between static $var, static::$var and self::$var:

class Base {
    const VALUE = 'base';

    static function testSelf() {
        // Output is always 'base', because `self::` is always class Base
        return self::VALUE;
    }

    static function testStatic() {
        // Output is variable: `static::` is a reference to the called class.
        return static::VALUE;
    }
}

class Child extends Base {
    const VALUE = 'child';
}

echo Base::testStatic();  // output: base
echo Base::testSelf();    // output: base

echo Child::testStatic(); // output: child
echo Child::testSelf();   // output: base

Also note that the keyword static has 2 quite different meanings:

class StaticDemo {
    static function demo() {
        // Type 1: `static` defines a static variable.
        static $Var = 'bar';

        // Type 2: `static::` is a reference to the called class.
        return static::VALUE;
    }
}
3
  • 1
    great explanation
    – doz87
    Commented Aug 25, 2018 at 13:20
  • Is it behaving the same when using static calls to static methods vs instanciating and calling object methods however? The "static" keyword in front of each method is concerning Commented May 1, 2019 at 12:46
  • @NaturalBornCamper there's no difference beterrn Child::testStatic() and $child::testStatic(). Though also $child->testStatic() works, you should stick to :: for static methods - using -> will throw a notice, and can be confusing for other developers
    – Philipp
    Commented May 2, 2019 at 7:44
5

Instead of

$c = get_class( $this );
echo $c::MY_CONST;

Do this

$c = get_class( $this );
echo constant($c . '::MY_CONST');
0

I couldn't get it to work with const as it prints "yonderyonder" (that's the thing about constants, they don't change), but it works fine with var:

<?php
class MyClass {

     var $MY_CONST = "yonder";

     public function __construct() {

     echo $this->MY_CONST;
     }
}

class ChildClass extends MyClass {

     var $MY_CONST = "bar";
}

$x = new ChildClass(); // prints 'bar'
$y = new MyClass(); // prints 'yonder'

?>
2
  • 2
    2015 - we don't need var Commented Dec 2, 2015 at 9:45
  • 3
    Yeah, this response was written 5 years ago, where it was still a thing!
    – Cetra
    Commented Dec 2, 2015 at 22:44
0

If you need to access constants, properties, methods of classes or objects you can use reflection, it provides much more details about structure of the object.
example:

class MainClass
{
    const name = 'Primary';

    public $foo = 'Foo Variable';
}
class ExtendedClass extends MainClass
{
    const name = 'Extended';
}

/**
 * From Class Name
 */

//get reflection of main class
$mainReflection = new ReflectionClass('MainClass');

if($mainReflection->hasConstant('name'))
    var_dump($mainReflection->getConstant('name'));//Primary

//get reflection of extended class
$extendedReflection = new ReflectionClass('ExtendedClass');

if($extendedReflection->hasConstant('name'))
    var_dump($extendedReflection->getConstant('name'));//Extended

/**
 * From Objects
 */
$main = new MainClass();
$extended = new ExtendedClass();

//get reflection of main class
$mainReflection = new ReflectionObject($main);

if($mainReflection->hasConstant('name'))
    var_dump($mainReflection->getConstant('name'));//Primary

//get reflection of extended class
$extendedReflection = new ReflectionObject($extended);

if($extendedReflection->hasConstant('name'))
    var_dump($extendedReflection->getConstant('name'));//Extended

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.