0

when I want to statically access the class JSON using an variable it is possible.

Code:

<?php
$classname = "JSON";
$classname::echo_output();
?>

But when I want to use an object variable to statically access the class it fails. code:

<?php
class someclass{
public $classname = "JSON";
    public function __construct(){
        $this->classname::echo_output();
    }
}
?>

Try it yourself.

How I solved it was $classname = $this->classname; But is there any other possible way to solve this?

2
  • 2
    Where is the "echo_output()" function defined? Commented May 16, 2014 at 17:26
  • From the looks of it you're using a custom JSON class and have already solved the problem you were experiencing. Is there another issue you're having? Commented May 16, 2014 at 17:27

4 Answers 4

2

You can use call_user_func function in order to achieve this

<?php

class someclass{

public $classname = "JSON";

    public function __construct(){
        call_user_func([$this->classname, 'echo_output']);
    }

}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

This way i cant add parameters can i?
@Ismail You actually can add parameters, just call it like so: call_user_func(['JSON', 'echo_output'], $param1, $param2, ... , $paramN); Look it up from the manual.
0

If echo_output actually exists in the class your calling, this should work but you have to assign the property to a variable first.

public function __construct(){
    $classname = $this->classname;
    $classname::echo_output();
}

1 Comment

You're right, I assumed it would work but then went to read up on it: php.net/manual/en/language.variables.variable.php
0

By PHP 5.4+ you can do this with ReflectionClass in a single line.

(new \ReflectionClass($this->classname))->getMethod('echo_output')->invoke(null);

under PHP 5.4

call_user_func(array($this->classname, 'echo_output'));

but I don't recommend this.

Instead of using static methods, you should create instances, inject them, and call their methods...

interface Helper{
    public function echo_output();
}

class JSONHelper implements Helper {
    ...
}

class someclass{
    public function __construct(Helper $helper){
        $helper->echo_output();
    }
}

new someclass(new JSONHelper()); //multiple instances
new someclass(JSONHelper::getInstance()); //singleton
new someclass($helperFactory->createHelper()); //factory
new someclass($container->getHelper()); //IoC container

Comments

0

By some research and javascript knowledge this solution would be the simpliest.

<?php
class someclass{
public $classname = "JSON";
    public function __construct(){
        $that = (array) $this;
        $that["classname"]::echo_output();
    }
}
?>

Just cast objects to arrays.

This way you don't have to define a variable for each dynamic classname.

Comments

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.