Reputation: 1405
I am curious as to why using the scope resolution operator on class variables causes a fatal php error, and if there is a way around it.
For example:
<?php
class StaticTest
{
public static function output()
{
echo "Output called<br />";
}
}
Class Test
{
public $reference;
public function __construct()
{
$this -> reference = new StaticTest;
}
}
$static_test = new StaticTest;
$static_test::output(); //works as intended
$test = new Test;
$test -> reference::output(); //Unexpcted T_PAAMAYIM_NEKUDOTAYIM
$direct_reference = $test -> reference;
$direct_reference::output(); //works, closest solution i have found, but requires the extra line of code / variable
?>
Upvotes: 4
Views: 1611
Reputation: 10114
If your only concern is the number of lines of code and the extra variable, here's how you can do it in one line, without creating any variables:
call_user_func(array(get_class($test->reference), 'output'));
Which, I believe, is equivalent to:
$direct_reference = $test->reference;
$direct_reference::output();
Upvotes: 3