让我来上一节课
class MyClass{
//class implementation
}我想将以下方法添加到这个类中:
public function addProperty($property){
//This method will be set a property of a current MyClass instance with the name **property** and the value **$property**
}有没有实现这个方法的想法?
发布于 2014-04-25 14:53:54
此方法将被设置为当前MyClass实例的属性,其名称为属性,值为$property
这是非常基本的,做:
public function addProperty($property){
$this->property=$property; // declare it as well
}或者,您的意思是属性只是一个占位符?然后你可以这样做
$this->{$property}=$property; 发布于 2014-04-25 14:54:04
public function addProperty($property){
$this->{$property} = $property;
}发布于 2014-04-25 14:56:53
只需在该类中编写方法,为该类创建一个对象。如果您想要使用该方法,只需使用该对象调用该方法
$obj = new MyClass; $return = $obj->addProperty();
在该方法中,如果您想使用属性,请像wise一样写下
public function addProperty($property){
$this->property = $property;
}它将为您提供属性值
https://stackoverflow.com/questions/23286085
复制相似问题