2010-03-12 20 views
22

Widziałem ten przykład z php.net:Jak uzyskać dostęp do stałej zdefiniowanej w klasie potomnej z funkcji klasy nadrzędnej?

<?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' 
?> 

Ale $ c :: MY_CONST ujmuje się tylko w wersji 5.3.0 lub nowszej. Klasa, którą piszę, może być bardzo rozproszona.

Zasadniczo zdefiniowałem stałą w klasie ChildClass, a jedna z funkcji w klasie MyClass (klasa ojca) musi korzystać ze stałej. Dowolny pomysł?

Odpowiedz

62

Jak na temat korzystania static::MY_CONST?

+1

Nie rozumiem, dlaczego ludzie sięgają głęboko w OOP w innych odpowiedziach. Twoje rozwiązanie jest jedyne słuszne i prostsze. –

+2

Coś jest nie tak z dostępem do 'const' przy użyciu słowa kluczowego' static'. Czy możesz wyjaśnić, dlaczego to działa? PHP Docs również mnie zdezorientowało. Dzięki. – pavlindrom

+0

Lub 'self :: MY_CONST' – checksum

0

nie mogę zmusić go do pracy z const, gdyż drukuje „yonderyonder” (to jest rzeczą stałe, nie zmienia się), ale działa dobrze z 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' 

?> 
+1

2015 - nie musimy var –

+1

Tak , ta odpowiedź została napisana 5 lat temu, gdzie wciąż była rzeczą! – Cetra

3

Zamiast

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

czy ta

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

Jeżeli chcesz mieć dostęp stałe, właściwości, metod klas lub obiektów, można użyć reflection, zapewnia znacznie więcej szczegółów na temat struktury obiektu.
przykład:

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 
0

od PHP 5.3:

Zastosowanie static::MY_CONST


Więcej szczegółów na static

W tym przypadku the keyword static jest odniesienie do rzeczywistości zwanej klasy.

To ilustruje różnicę między static $var, static::$var i 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 

uwagę, że kluczowe static ma 2 zupełnie różne znaczenia też:

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; 
    } 
} 
Powiązane problemy