Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't call static method from class as variable name?

I'm using php 5.2.6. I have a strategy pattern, and the strategies have a static method. In the class that actually implements one of the strategies, it gets the name of the strategy class to instantiate. However, I wanted to call one of the static methods before instantiation, like this:

$strNameOfStrategyClass::staticMethod();

but it gives T_PAAMAYIM_NEKUDOTAYIM.

$> cat test.php

<?

interface strategyInterface {
        public function execute();
        public function getLog();
        public static function getFormatString();
}


class strategyA implements strategyInterface {
        public function execute() {}
        public function getLog() {}
        public static function getFormatString() {}
}

class strategyB implements strategyInterface {
        public function execute() {}
        public function getLog() {}
        public static function getFormatString() {}
}

class implementation {
        public function __construct( strategyInterface $strategy ) {
                $strFormat = $strategy::getFormatString();
        }
}

$objImplementation = & new implementation("strategyB") ;

$> php test.php

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /var/www/test.php on line 24

$> php -v

PHP 5.2.6-1+lenny9 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug  4 2010 03:25:57)

Would this work in 5.3?

like image 973
user151841 Avatar asked Sep 24 '10 14:09

user151841


People also ask

Why static method Cannot call non-static variables?

Namely, static methods can only use static variables and call static methods—they cannot access instance variables or methods directly, without an object reference. This is because instance variables and methods are always tied to a specific instance, i.e., object of their class.

Can a static method be called by object?

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.

Why can't static methods call the instance methods present in the same class?

A static method is used as a utility method, and it never depends on the value of an instance member variable. Because a static method is only associated with a class, it can't access the instance member variable values of its class.

Why are static methods unable to access non-static variables and non-static methods within the class?

A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method should operate on.


1 Answers

Yes. That syntax was introduced in 5.3

To workaround for <= 5.2, you can use call_user_func:

call_user_func(array($className, $funcName), $arg1, $arg2, $arg3);

or call_user_func_array:

call_user_func_array(array($className, $funcName), array($arg1, $arg2, $arg3));

But on another note, what you're trying to do doesn't really make sense...

Why have it as a static function? Your constructor in implementation is expecting an object anyway (that's what strategyInterface $strategy is looking for). Passing a string won't work, since strings don't implement interfaces. So what I would do, is make the interface non-static, and then do something like:

$strategy = new StrategyB();
$implementation = new Implementation($strategy);

Then, in the constructor:

$strFormat = $strategy->getFormatString();

Or, if you really still want that method to be static you could do:

$strFormat = call_user_func(array(get_class($strategy), 'getFormatString'));

Oh, and = & new synax is deprecated (and doesn't do what you think it does anyway).

like image 79
ircmaxell Avatar answered Sep 25 '22 14:09

ircmaxell