我想我在理解类中的构造函数时遇到了麻烦。我在wordpress插件中编写了一个类。我现在想在另一个插件中使用该类。这都很好。在我的函数中,我可以做新的类等等,然后做我的事情。但是,我希望能够做的是将一些值传递到这个新的类构造函数文件中,然后从构造函数传递变量。所以像这样:
class why_wont_you_work {
public foo1;
public foo2;
public function __construct() {
$this->foo1= $otherclassfunction['name'];
$this->foo2= $otherclassfunction['address'];
public function do_stuff() {
$otherclass = new $otherclass;
$otherclassfunction = $otherclass->otherclassfunction();
echo $this->foo1;
}
您可以创建一个接受参数的构造函数,并从创建参数的位置传递它们,就像函数的正常情况一样。
class why_wont_you_work {
public foo1;
public foo2;
public function __construct($name, $address) {
$this->foo1= $name;
$this->foo2= $address;
public function do_stuff() {
$otherclass = new $otherclass;
$otherclassfunction = $otherclass->otherclassfunction();
echo $this->foo1;
}
$obj = new why_wont_you_work( "some name", "some address" );