In the PHP documentation , I found this code against the scope resolution operator ::
<?php
class OutraClasse extends MinhaClasse {
public static $meu_estatico = 'variável estática';
public static function doisPontosDuplo() {
echo parent::VALOR_CONST . "\n";
echo self::$meu_estatico . "\n";
}
}
$classname = 'OutraClasse';
echo $classname::doisPontosDuplo(); // No PHP 5.3.0
OutraClasse::doisPontosDuplo();
?>
By doing a test here, I realized that turning $classname
into static attribute and changing the call of the variable like this:
class OutraClasse {
public static $meu_estatico = 'variável estática';
public static $classname = 'OutraClasse';
public static function doisPontosDuplo() {
echo parent::VALOR_CONST . "\n";
echo self::$meu_estatico . "\n";
}
public static function teste(){
echo self::$classname::doisPontosDuplo();
}
}
OutraClasse::teste();
PHP throws the following error:
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
Why does the first form pass and the second PHP throws a parse error?
Note: When you have an object inside another type $obj1->getObj2->getMetodoObj2
, php does not throw an error.