当我在对象中时,如何使用对象(及其方法和属性)?
说我有这样无用的课程:
class Fruit {
private $name; // Name of the fruit.
private $health = 10; // 0 is eaten, 10 is uneaten.
private $object; // This is a PHP object.
public function __construct($name) {
$this->name = $name;
}
public function set($varname,$value) {
$this->$varname = $value;
}
}
class Eater {
private $name;
public function eat($object) {
$object->set('health',0); // I know I can pass and modify objects like this.
// The object is passed by reference in PHP5 (but not 4), right?
}
}
我这样使用它:
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->eat($apple);
?>
但是如果我像这样修改Eater类:
class Eater {
private $name;
private $objectToEat; // Let's say if I need the object to be over here instead of in a method.
public function set($varname,$value) {
$this->$varname = $value;
}
public function eat() {
$this->objectToEat->set('health',0); // This doesn't work!
}
}
并设置主程序如下:
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->set('objectToEat',$apple);
$paul->eat();
?>
如何从方法内部访问对象的属性?我知道我使用$this->objectToEat
告诉PHP我正在谈论类的正确性,但由于该属性是一个对象,我如何访问该对象的方法?
我已经尝试$this->objectToEat->set('health',0)
但这不起作用。我希望你们能理解我想要达到的目标(抱歉,我无法弄清楚如何在不影响清晰度的情况下压缩我的问题)!
答案 0 :(得分:1)
您必须正确设置属性。由于它是private
,你无法从对象外部执行此操作,因此必须使用封装:
class Eaters {
private $name;
private $objectToEat;
public function eat() {
$this->objectToEat->set('health',0); // Assumed "object" was just a typo
}
public function setObjectToEat($object) {
$this->objectToEat = $object;
}
}
然后像这样使用它:
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->setObjectToEat($apple);
$paul->eat();
?>
注意:在这个简短的示例中,您的原始方法是更好的设计。在某些情况下,您可能希望通过预先设置属性来填充要使用的方法,但更常见的是您希望直接使用参数调用它,因为它更清晰且更可重用(划分区域)。
答案 1 :(得分:0)
这可能是因为你的eat方法不接受任何参数,而且Eaters类没有$ object属性。
答案 2 :(得分:0)
class Tester {
private $variable;
private $anObj;
public function testFn($val) {
$this->variable = $val;
$this->anObj = new SecondObj();
$this->doSomething();
}
public function doSomething() {
echo("My variable is set to " . $this->variable);
$this->anObj->wow();
}
}
class SecondObj {
public function __construct() {
echo("I'm new!");
}
public function wow() { echo("Wow!"); }
}
$tester = new Tester();
$tester->testFn(42);
输出:
I'm new!My variable is set to 42Wow!
答案 3 :(得分:0)
你能将$ objectToEat作为引用,然后在eat()函数中使用它吗?
答案 4 :(得分:0)
你必须在课堂上设置$ this-&gt;对象
function __construct($object){
$this->object = $object;
}
或
<?php
$pear = new Fruit("Pear");
$apple = new Fruit("Apple");
$paul = new Eater("Paul");
$paul->eat($apple);
?>
答案 5 :(得分:0)
此答案修改了Renesis的回答
在课堂上,吃的对象是一个私有变量因此你不能去
$paul->objectToEat = $apple;
你可以做的是在Eaters中制作一个setter方法
class Eaters {
private $name;
private $objectToEat;
public function eat() {
$this->objectToEat->set('health',0); // Assumed "object" was just a typo
}
public function setFood($object) {
$this->objectToEat = $object;
}
}
因此,您可以改为调用setFood()
方法。
OR
将eat()
更改为
public function eat($object) {
$this->object->set('health',0);
return $object;
}
将修改后的对象保存回原始变量。
OR
class Eaters {
private $name;
public function eat(&$object) { // this passes object by reference
$object->set('health', 0);
}
}
虽然未对此代码进行测试,但您可以通过引用传递变量。
注意:在定义方法时,只需要&
,而不是在传递参数时。有关以参考方式传递的详细信息,请转至this link