开发者

Calling a class's constant in another class's variable

I was wondering if there is any possibility in PHP to do following;

<?php

class boo {
 static public $myVariable;

 public function __construct ($variable) {
   self::$myVariable = $variable;
 }
}

class foo {
  public $firstVar;
  public $secondVar;
  public $anotherClass;

 public function __construct($configArray) {
   $this->firstVar = $configArray['firstVal'];
   $this->secondVar= $configArray['secondVar'];
   $this->anotherClass= new boo($configArray['thirdVal']);
 }
}

$classFoo = new foo (array('firstVal'=>'1st Value', 'secondVar'=>'2nd Value', 'thirdVal'=>'Hello World',));

echo $classFoo->anotherClass::$myVariable;
?>

Expected OUTPUT : Hello World

I am getting following error; Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM

I Googled and it is related to colon (double dots) in $classFoo->anotherClass::$myVariable

I wouldn't like to go all the trouble to change my other classes. Is there anyway around this problem?

Thank you for your help in advance.

P.S. I just didn't want to lose few hour开发者_C百科s on this to find a way around. I already spent yesterday 2.5 hours to change almost whole Jquery because customer wanted a change and today in the morning I was asked to take the changes back because they didn't want to use it (they changed their mind). I am just trying to avoid big changes right now.


You need to do:

$anotherClass = $classFoo->anotherClass;
echo $anotherClass::$myVariable;

Expanding expressions to class names/objects for static calls/constants is not supported (but expanding variables, as shown above, is).


If you do not care about memory and execution speed, this is correct.
It seems that reference would be better:

$classRef = &$classFoo->anotherClass;
echo $classRef;

Works for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜