【发布时间】:2011-03-07 03:31:41
【问题描述】:
我已经看过并尝试过,但我找不到答案。
在 PHP 中,是否可以调用类的成员函数(当该类需要构造函数来接收参数时)而不将其实例化为对象?
一个代码示例(给出错误):
<?php
class Test {
private $end="";
function __construct($value) {
$this->end=$value;
}
public function alert($value) {
echo $value." ".$this->end;
}
}
//this works:
$example=new Test("world");
$example->alert("hello");
//this does not work:
echo Test("world")::alert("hello");
?>
【问题讨论】:
-
我以为 echo Test::__construct("world")::alert("hello");可能有效,但它没有,叹息
-
将 alert() 设置为静态函数就可以了。
标签: php class constructor methods instantiation