我有一个主要的PHP类,如:
Application.EnableEvents = False
有一种方法,存储在class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass();
$this->b = 'some string';
}
}
($a
)属性中的类可以访问SomeClass
值,该值实际上是存储在启动{的类中的属性{1}}($b
)?
答案 0 :(得分:1)
你可以这样做:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass($this);
$this->b = 'some string';
}
}
class SomeClass {
public $mc;
function __construct(MyClass $mc)
{
$this->mc = $mc;
}
}
$myClass = new MyClass();
echo $myClass->a->mc->b;
输出结果为:some string
答案 1 :(得分:0)
您可以这样做:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->b = 'some string';
$this->a = new \SomeClass($this->b);
}
}
class SomeClass{
function __construct($b)
{
echo $b; // it will become a $this->b as given while creating new class in MyClass
}
}