PHP 5.3 ve '::'

4 Cevap

Ben 5.3 ile PHP içine başladı ve kullanıyorum '::' sabitleri eski erişmek için; sınıf :: const. Ben eski bir PHP 5.1.6 ve 5.2.12 yani kodumu kullanmaya çalıştığınızda, ancak, ben '::' beklenmedik bir hata alıyorum.

Nasıl PHP5'ta bu eski sürümlerinde sabitleri erişebilirim?

4 Cevap

ClassName::constant çalışmalıdır. Göre the documentation, aşağıdaki sözdizimi PHP 5.3 yeni:

$classname = "MyClass";
echo $classname::constant . "\n"; // As of PHP 5.3.0

$class = new MyClass();
$class->showConstant();

echo $class::constant."\n"; // As of PHP 5.3.0

Daha tam bir kod örneği / azaltma hata ayıklama ile yardımcı olabilir.

Bu olmalıdır:

ClassName::CONSTANT_NAME

Bu PHP 5 tüm sürümlerinde çalışması gerekir.

With the :: operator you can call only the static methods or access to the static variables/constants of a class.
The proper way is className::method() or className::publicVariable. Inside the static methods you can't refer to this because it's not called on a object, but from a non-static method you can access to a static varibale.

Neyse, :: operator yaklaşık PHP 5.3 sadece yeni özellik Sınıfadı içeren bir $ dize kullanmak için yeteneğidir.

Mesaj kod Kolelityazis kısmını memnun

Ben sınıf ismi yoluyla sınıf sabitlerini erişirken aynı sorun bu yüzden alıcılar için başvurdu vardı:

public function getSomeConstant() {
    return self::SomeConstant;
}

ve bölgelerinde bunu gerektiğinde:

className::getSomeConstant();

Edit: