在另一个类的变量中调用一个类的常量

发布于 2024-12-06 14:29:28 字数 1081 浏览 3 评论 0原文

我想知道 PHP 是否有可能执行以下操作;

<?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;
?>

预期输出: Hello World

我收到以下错误; 解析错误:语法错误,意外的T_PAAMAYIM_NEKUDOTAYIM

我用Google搜索了一下,它与$classFoo->anotherClass::$myVariable中的冒号(双点)有关code>

我不想费尽心思去改变我的其他类。无论如何,这个问题有解决办法吗?

提前感谢您的帮助。

PS 我只是不想为此浪费几个小时来寻找解决方法。昨天我已经花了 2.5 个小时来更改几乎整个 Jquery,因为客户想要更改,今天早上我被要求收回更改,因为他们不想使用它(他们改变了主意)。我现在只是想避免发生重大变化。

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 hours 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄟ。诗瑗 2024-12-13 14:29:28

您需要执行以下操作:

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

不支持将表达式扩展为静态调用/常量的类名/对象(但支持扩展变量,如上所示)。

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).

绝情姑娘 2024-12-13 14:29:28

如果您不关心内存和执行速度,这是正确的。
看来参考会更好:

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

对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文