php面向对象编程示例学习笔记

WBOY
Libérer: 2016-08-08 09:27:05
original
993 Les gens l'ont consulté

1、__get()和__set()方法

<!--
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.
-->

    
        <meta charset="UTF-8">
        <title></title>
    
    
        <?php // put your code here
        class animal{
            private $name;
            private $color;
            private $age;
            public function __get($property_name) {
                echo "在直接获取私有属性值的时候,自动调用了这个__get()方法<br>";
                if(isset($this->$property_name)){
                    return $this->$property_name;
                }
                else
                {
                    return NULL;
                }
            }
            
            public function __set($propertyname, $value) {
                echo "在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值<br>";
                $this->$propertyname = $value;
            }
        }
        $pig = new animal();
        $pig->name = "猪";
        $pig->color = "白色";
        $pig->age = "1岁";
        echo "称呼:".$pig->name."<br>";
        echo "颜色:".$pig->color."<br>";
        echo "年龄:".$pig->age."<br>";
        ?>
    

Copier après la connexion

2、__call()方法

<!--
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.
-->

    
        <meta charset="UTF-8">
        <title></title>
    
    
        <?php // put your code here
        class Test{
            function __call($function_name, $args) {
                print "你所调用的函数:$function_name(参数:";
                print_r($args);
                print ")不存在!<br>\n";
            }
        }
        $test = new Test();
        $test->demo("one", "two", "three");
        echo "this  is a test<br>";
        ?>
    

Copier après la connexion

3、clone对象

<!--
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.
-->

    
        <meta charset="UTF-8">
        <title></title>
    
    
        <?php // put your code here
         class animal{
             //下面是动物的成员属性
             public $name = &#39;&#39;;//成员属性name
             public $color = &#39;&#39;; //成员属性color
             public $age = &#39;&#39;; //成员属性size
             
             //定义一个构造方法, 参数为$name、$color和$age
             function __construct($name, $color, $age) {
                 //通过构造方法传进来的$name 给成员属性$this->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();
?>
    

Copier après la connexion

4、__clone()方法

<!--
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.
-->

    
        <meta charset="UTF-8">
        <title></title>
    
    
        <?php // put your code here
         class animal{
             //下面是动物的成员属性
             public $name = &#39;&#39;;//成员属性name
             public $color = &#39;&#39;; //成员属性color
             public $age = &#39;&#39;; //成员属性size
             
             //定义一个构造方法, 参数为$name、$color和$age
             function __construct($name, $color, $age) {
                 //通过构造方法传进来的$name 给成员属性$this->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();
?>
    

Copier après la connexion

5、__toString()方法

<!--
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.
-->

    
        <meta charset="UTF-8">
        <title></title>
    
    
        <?php // put your code here 
        //Declare a simple class
        class TestClass{
            public $foo;
            public function __construct($foo) {
                $this->foo = $foo;
            }
            
            public function __toString() {
                return $this->foo;
            }
        }
        $class = new TestClass('HelloWorld');
        echo $class;
        ?>
    

Copier après la connexion

6、const关键字

<!--
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.
-->

    
        <meta charset="UTF-8">
        <title></title>
    
    
        <?php // put your code here
        class MyClass{
            const constant = &#39;constant value&#39;;
            function showConstant(){
                echo self::constant."\n";
            }
        }
        echo MyClass::constant."\n";
        $class = new MyClass();
        $class->showConstant();
        ?>
    

Copier après la connexion

以上就介绍了php面向对象编程示例学习笔记,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal