尝试访问php中包含对象的属性 [英] Trying to access properties of the containing object in php
问题描述
经过几个小时寻找解决方案和模式,现在是时候问问专业人士了。
我想在逻辑层次结构中订购我的对象,想要能够访问父对象的属性。一个简单的例子,我喜欢它的工作...
class car {
public $ strType; //保存字符串
public $ engine; //保存另一个类的实例
public function __construct(){
$ this-> type =Saab;
//尝试传递$ this以使其在$ this-> engine中可访问
$ this-> engine = new engine($ this);
}
}
class engine {
public $ car;
public function __construct($ parent){
$ this-> car = $ parent;
}
public function start(){
//这里是我想要访问汽车属性和方法...
echo $ this > car-> $ strType;
}
}
$ myCar = new car();
$ myCar-> engine-> start();
我不会实现的是引擎中的方法可以访问父汽车属性。
我设法这样做,但我相信这是非常丑陋...
$ myCar =新汽车();
$ myCar-> addParent($ myCar);
在addParent中,MethodI可以将实例传递给引擎对象。
但是这不能是线索,可以吗?是我的整个想法奇怪吗?
我不想让引擎从汽车继承,因为一个汽车有很多方法和属性和引擎没有。
正如@Wrikken所提到的,正确的语法是 echo $ this-> car-> strType;
类似似乎不是
car
的成员,但如果您更改到
$ this-> strType =Saab
然后,所讨论的语句现在应该回应Saab
虽然我认为这里的好习惯是没有引擎类包含一个汽车对象,只是汽车类应该包含一个引擎对象。而且属性会更好,因为 private
。所以你可以有一个汽车方法像
public startEngine(){
$ success = $ this-> engine - > start();
if(success){
echoEngine started successfully!;
} else {
echoEngine is busted!;
}
}
其中 engine :: start ()
返回一个布尔值。
After hours looking for solutions and patterns, it's time for me to ask the pros.
I'd love to order my object in a logical hierarchy, but still want to be able to access properties of the parent object. A simple example how I'd love to have it work...
class car {
public $strType; // holds a string
public $engine; // holds the instance of another class
public function __construct(){
$this->type = "Saab";
// Trying to pass on $this to make it accessible in $this->engine
$this->engine = new engine($this);
}
}
class engine {
public $car;
public function __construct($parent){
$this->car = $parent;
}
public function start(){
// Here is where I'd love to have access to car properties and methods...
echo $this->car->$strType;
}
}
$myCar = new car();
$myCar->engine->start();
What I won't achieve is that a method in engine can access the 'parent' car properties. I managed to do so like this, but I believe that is very very ugly...
$myCar = new car();
$myCar->addParent($myCar);
From within the addParent MethodI would be able to pass the instance on to the engine object. But that can't be the clue, can it? Is my whole idea queer?
I don't want engine to inherit from car because a car has lots of methods and properties and engine has not. Hope you get what I mean.
Hoping for hints, Cheers Boris
As @Wrikken mentioned, the correct syntax would be echo $this->car->strType;
type
does not seem to be a member of car
, but if you changed it the line to
$this->strType = "Saab";
Then the statement in question should now echo "Saab"
Though I think good practice here would be to not have the engine class contain a car object, just the car class should contain an engine object. And the properties would be better off as private
. So you could have a car method like
public startEngine() {
$success = $this->engine->start();
if(success) {
echo "Engine started successfully!";
} else {
echo "Engine is busted!";
}
}
Where engine::start()
returns a boolean.
这篇关于尝试访问php中包含对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!