I'm trying call class method inside another class.
<?php
class A {
public static $instant = null;
public static $myvariable = false;
private function __construct() {}
public static function initial() {
if (static::$instant === null) {
$self = __CLASS__;
static::$instant = new $self;
}
return static::$instant; // A instance
}
}
class B {
private $a;
function __construct($a_instance) {
$this->a = $a_instance;
}
public function b_handle() {
// should be:
$this->a::$myvariable = true;
// but:
// Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
// try with:
// $this->a->myvariable = true;
// but:
// Strict Standards: Accessing static property A::$myvariable as non static
}
}
// in file.php
$b = new B(A::initial());
$b->b_handle();
var_dump(A::$myvariable);
for now, my alternative is:
<?php
class A {
public function set_static($var,$val) {
static::$$var = $val;
}
}
so:
$this->a->set_static('myvariable',true);
what should I do ? what is happen ? am I wrong ?
why I cannot set myvariable as static variable direcly from B class ?
sorry for bad english.
__construct()method forBand you'll see one of several problems immediately:var_dump($a_instance);I getNULLprinted out, so your code does not do what you think it does.$myvariableis a static property. Which is assigned using aclassname::syntax.static::$instant !== null- so if it's NOT null you create new one. And if it's null you just return null? Are you sure?