1

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.

5
  • This is highly unusual code, and I think there are several issues here that are confusing you and other possible answerers here. Try putting this into your __construct() method for B and you'll see one of several problems immediately: var_dump($a_instance); I get NULL printed out, so your code does not do what you think it does. Commented Dec 20, 2015 at 16:29
  • $myvariable is a static property. Which is assigned using a classname:: syntax. Commented Dec 20, 2015 at 16:33
  • And 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? Commented Dec 20, 2015 at 16:35
  • oh, sorry wrong code in A::initial(), should be if (static::$instant === null) Commented Dec 20, 2015 at 16:35
  • @u_mulder , i'did. A instantce from A::initial() Commented Dec 20, 2015 at 16:39

2 Answers 2

2

Refer to the manual: http://php.net/manual/en/language.oop5.static.php

A property declared as static cannot be accessed with an instantiated class object (though a static method can).

So, you cannot in anyway set static property from class instance.

As an option you can add some setter, which set your static variable:

public function setMyVar($value) {
    static::$myvariable = $value;
}

$this->a->setMyVar(true);
Sign up to request clarification or add additional context in comments.

Comments

1

Your call,

$this->a::$myvariable = true;

The PHP manual says,

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

This is why you are unable to assign static property through an object.

Simply do this:

A::$myvariable = true;

Here's the reference:

1 Comment

but, in my 'real' code, I using namespace, and i wouldn't re-import (in this case ) A class using 'use'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.