PHP 5.6. * Vs 7.0. * Syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)

It is strange that my research did not change this exact scenario:

$someVar = $this->StaticClassName::$staticClassProperty;

php 7. * happily accesses the property, but 5.6. * (.11 in this case) crashes with an error:

unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)

And yes, I tried every $ {permutation} :: I might think.

+4
source share
2 answers

There are a lot of complex expressions that don't work in PHP 5. Usually the solution is to split it into multiple expressions, and you can do it here:

$className = $this->StaticClassName;
$someVar = $className::$staticClassProperty;

This works on both PHP 5 and PHP 7.

+4
source

Well, here is at least one solution that may be acceptable for your needs:

<?php 

class MyClass {

    public $childClass;

    public function __construct() {

        $this->childClass = new ChildClass();

    }

}

class ChildClass {

    public static $foo = 'bar';

    public function getFoo() {

        return static::$foo;

    }

}

$obj = new MyClass();

echo $obj->childClass->getFoo();

?>
0
source

Source: https://habr.com/ru/post/1649600/


All Articles