时间:2021-07-01 10:21:17 帮助过:20人阅读
";
if(isset($this->$property_name)){
return $this->$property_name;
}
else
{
return NULL;
}
}
public function __set($propertyname, $value) {
echo "在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值
";
$this->$propertyname = $value;
}
}
$pig = new animal();
$pig->name = "猪";
$pig->color = "白色";
$pig->age = "1岁";
echo "称呼:".$pig->name."
";
echo "颜色:".$pig->color."
";
echo "年龄:".$pig->age."
";
?>
\n";
}
}
$test = new Test();
$test->demo("one", "two", "three");
echo "this is a test
";
?>
name赋初值
$this->name = $name;
//通过构造方法传进来的$color 给成员属性$this->color赋初值
$this->color = $color;
//通过构造方法传进来的$age 给成员属性$this->age赋初值
$this->age = $age;
}
function getInfo(){
echo '动物的名字叫做'.$this->name.',动物的颜色是'.$this->color.',动物的年龄是'.$this->age.'.';
}
}
$pig = new animal("猪", "白色", "1岁");
//使用clone克隆新对象pig2,和$pig对象具有相同的属性和方法
$pig2 = clone $pig;
$pig2->getInfo();
?>
name赋初值
$this->name = $name;
//通过构造方法传进来的$color 给成员属性$this->color赋初值
$this->color = $color;
//通过构造方法传进来的$age 给成员属性$this->age赋初值
$this->age = $age;
}
function getInfo(){
echo '动物的名字叫做'.$this->name.',动物的颜色是'.$this->color.',动物的年龄是'.$this->age.'.';
}
function __clone() {
//$this指的复本pig2,而$that是指向原本pig,这样就在本方法中改变了复本的属性;
$this->name = "假的$this->name";
$this->age = '2岁';
}
}
$pig = new animal("猪", "白色", "1岁");
//使用clone克隆新对象pig2,和$pig对象具有相同的属性和方法
$pig2 = clone $pig;
$pig->getInfo();
$pig2->getInfo();
?>
foo = $foo;
}
public function __toString() {
return $this->foo;
}
}
$class = new TestClass('HelloWorld');
echo $class;
?>
showConstant();
?>
以上就介绍了php面向对象编程示例学习笔记,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。