php.jpg


<?php
header("Conntent-Type:text/html;charset=utf-8");
class animla{
    private $name;
    private $color;
    private $age;
    public function __get($property_name) {
        if(isset($this->$property_name)){
            return $this->$property_name;
        }else{
            return(NULL);
        }
    }
    public function __set($property_name, $value) {
        $this->$property_name = $value;
    }
}
$dog=new animla();
$dog->name="小狗";
$dog->color="白色";
$dog->ahe=4;
echo $dog->name."<br>";
echo $dog->color."<br>";
echo $dog->age."<br>";


<?php
class MyClass{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';
    function printHello(){
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
$obj = new MyClass();
echo $obj->public;
$obj->printHello();
class MyClass2 extends MyClass{
    protected $protected = 'Protected2';
    function printHello(){
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
$obj2 = new MyClass2();
echo $obj2->public;
echo $obj2->private;
$obj2->printHello();
?>


<?php
class animal{
    private $name;
    private $color;
    private $age;
    function __get($property_name) {
        if(isset($this->$property_name)){
            return ($this->$property_name);
        }else{
            return(NULL);
        }
    }
    function __set($property_name, $value) {
        $this->$property_name = $value;
    }
    function __isset($property_name) {
        return isset($this->$property_name);
    }
    function __unset($property_name) {
        unset($this->$property_name);
    }
}
$pig = new animal();
$pig->name = "小猪";
echo var_dump(isset($pig->name))."<br>";
echo $pig->name."<br>";
unset($pig->name);
echo $pig->name;


<?php
header("Content-Type:text/html;charset=utf-8");
class animal{
    private $name;
    private $color;
    private $age;
    private static $sex = "雄性";
    public function __construct($name,$color,$age) {
        $this->name = $name;
        $this->color = $color;
        $this->age = $age;
    }
    public function getInfo(){
        echo "动物的名称:".$this->name."<br>";
        echo "动物的颜色:".$this->color."<br>";
        echo "动物的年龄:".$this->age."<br>";
        self::getSex();
    }
    private static function getSex(){
        echo "动物的性别:".self::$sex;
    }
}
$dog = new animal("小狗","黑色",4);
$dog->getInfo();
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



<?php
class animal{
    private $name;
    private $color;
    private $age;
    public function __construct($name,$color,$age) {
        $this->name=$name;
        $this->color=$color;
        $this->age=$age;
    }
    public function __getInfo(){
        echo '名字:'.$this->name.',颜色:'.$this->color.',年龄:'.$this->age.',<br>';
    }
    public function __clone(){
        $this->name = "狗";
        $this->color="黑";
        $this->age="2岁";
    }
}
$pig = new animal('猪','白','1岁');
$pig->getInfo();
$pig2 = clone $pig;
$pig2->getInfo();
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */