4
In the PHP documentation, found that code in relation to the target 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();
?>
Taking a test here, I realized that turning $classname
in static attribute and change the variable call this way:
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 the first way you pass and the second way PHP throws a parse error?
Obs.: When you have an object inside another type $obj1->getObj2->getMetodoObj2
, php does not launch error.
Where have you changed it? How has it changed what has changed?
– Maniero
@bigown edited it. Now netbeans is saying syntax error. Good because I’d like to know why it’s a syntax error too.
– user28595
In the very PHP documentation that you put in says that 'self' serves to access content from within the class, in this second step you are using outside.
– Flavio Andrade
In the first, you make a direct reference to the class
OutraClasse
and then call the static method::doisPontosDuplo
, already in the second example, the variable - now property -$classname
doesn’t even exist.– Edilson
@Flavioandrade even within the class, error, see the edition.
– user28595
So Diego, as @Edilson put down, the line
self::$classname::doisPontosDuplo()
does not work even now being inside the class because the self refers to the class itself, so if we translate this your code the result would be something like:OutraClasse::OutraClasse::doisPontosDuplo()
what is wrong. In this case either you use only the selfself::doisPontosDuplo()
or the class nameOutraClasse::doisPontosDuplo()
.– Flavio Andrade