Php 调用类实例的方法
请原谅我问了这样一个新手问题,但我不知道如何在PHP中调用方法。以下是我在伪代码中尝试做的事情:Php 调用类实例的方法,php,Php,请原谅我问了这样一个新手问题,但我不知道如何在PHP中调用方法。以下是我在伪代码中尝试做的事情: class Thing { public string Color() { return "taupe"; } } Thing x = new Thing(); echo x.Color(); 结果应该是灰褐色的。最后一行是我遇到的问题:调用x的Color方法。在PHP中如何做到这一点?它是$x->Color;。PHP使用->而不是其他语言中的点来调用实例方法。
class Thing {
public string Color() {
return "taupe";
}
}
Thing x = new Thing();
echo x.Color();
结果应该是灰褐色的。最后一行是我遇到的问题:调用x的Color方法。在PHP中如何做到这一点?它是$x->Color;。PHP使用->而不是其他语言中的点来调用实例方法。
另外,您的代码看起来不像PHP
事物x=新事物;应该是$x=新事物
公共字符串Color{应该看起来像公共函数Color{它是$x->Color;。PHP使用->而不是其他语言中的点来调用实例方法。
另外,您的代码看起来不像PHP
thingx=新事物;应该是$x=新事物
公共字符串颜色{应该看起来像公共函数颜色{这里是一个示例
$x = new Thing(); //Instantiate a class
echo $x -> Color(); //call the method
这是一个例子
$x = new Thing(); //Instantiate a class
echo $x -> Color(); //call the method
试试这个:
$thing = new Thing();
echo $thing->Color();
试试这个:
$thing = new Thing();
echo $thing->Color();
在PHP中,您可以执行以下操作:
class Thing {
public function color() {
return "taupe";
}
}
$thing = new Thing;
echo $thing->color();
你很接近:
我建议你继续读下去。他们有很多关于如何设置对象和不同模式等的好信息
祝您好运!在PHP中,您可以执行以下操作:
class Thing {
public function color() {
return "taupe";
}
}
$thing = new Thing;
echo $thing->color();
你很接近:
我建议你继续读下去。他们有很多关于如何设置对象和不同模式等的好信息
祝你好运