Php面向对象学习笔记 ? 构造、析构、对象赋值、克隆

WBOY
Release: 2016-06-23 13:51:31
Original
730 people have browsed it

Php面向对象学习笔记 ? 构造、析构、对象赋值、克隆

 

class student

{

       public  $stu_id;       // 定义成员变量

       public  $stu_name;

      

       public  function sayName()   // 成员函数

       {

              echo $this->stu_name;   // $this  访问该对象的成员变量

       }

 

}

 

 

$stu = new  Student;      // 创建对象

$stu->stu_id = ‘0607001’;

$stu->stu_name = ‘小李’;

 

$stu->sayName();

 

 

 

 

注意:

 

 

 class  student

{

       public $stu_id;       // 定义成员变量

       public $stu_name;

      

       public function sayName()   // 成员函数

       {

              var_dump($stu_name);

              var_dump($GLOBALS[‘stu_name’]);   // 两种方法都无法访问成员变量

              //只要使用$this-> 才能访问成员变量

       }

 

}

 

 

$stu = new  Student;      // 创建对象

$stu->stu_id = ‘0607001’;

$stu->stu_name = ‘小李’;

 

$stu->sayName();

 

注:类中定义的属性,不相当于类中定义的全局变量,不能直接再方法中使用属性变量的形式访问。

 

 

 

1.   构造和析构:

php的opp机制,在new完成时,会试着调用一个叫做__construct()的方法。

如果我们将初始化的代码,写到这个方法内,就可以完成自动初始化。

 

例子:

 

class Student

{

        public $stu_id;

        public $stu_name;

       

        public function__construct($id,$name)      // 构造

        {

               $this->stu_id= $id;

               $this->stu_name= $name;

        }

}

 

$stu = new Student(‘100511101’,’songyang’);

 

注:如果构造方法没有参数,则 $stu = new Student 和 $stu = new Student() 都是对的。

 

 

构造方法的兼容性问题:

php5,构造方法的名字,就是__construct().在php5之前,构造方法的名字为与类同名。为了兼容,也同时支持与类同名的构造方法。

如果同时出现__construct() 和类名的构造方法:

 

例子:

 

class Student

{

       public $stu_id;

       public $stu_name;

 

 

       public function __construct()

       {

              echo“construct run …”;

       }

      

       public function Student()

       {

              echo‘’Student run …”;

       }

{

 

 

$stu = new Student;

 

输出:construct run …

 

结论:如果同时出现两种构造方法,调用__construct()

 

 

析构:

 

在对象被销毁时,也会自动执行一个方法。

 

析构方法名字为:__destruct();

 

 

class Student

{

       public $stu_id;

       public $stu_name;

      

       public function __construct()

       {

              echo“构造方法调用”;

       }

      

       //析构

       publicfunction __destruct()

       {

              //释放资源

              echo“析构方法调用”;

       }

}

 

该方法,用于释放,该对象所占用的额外资源,不是对象本身的内存空间!

 

什么情况下,对象会被销毁:

1.    脚本周期结束,对象自动被销毁。

2.    销毁保存该对象的变量。

$stu = new Student;

unset($stu);

 

输出:析构方法调用

 

3.    保存对象的变量,被赋值了其他数据。

$stu = new Student;

$stu = “new Value”;   // 赋值其他数据时,Student对象被销毁。

 

对象间的赋值

 

对象支持引用传递,不用&符号,因此不能通过=赋值的形式,得到一个新的对象。

 

例子

 

class Student

{

       public $stu_id;

       public $stu_name;

 

       public function __construct($id,$name)

       {

              $this->stu_id= $id;

              $this->stu_name= $name;

       }

}

 

$stu1 = new Student(“100”,”song”);

$stu2 = $stu1;

echo $stu1->stu_name;

echo $stu2->stu_name;

$stu1->stu_name = “songyang”;

echo $stu1->stu_name;

echo $stu2->stu_name;

 

 

输出:song song  songyang songyang

 

 

克隆

利用已有对象,得到相同的新对象。

需要使用关键字 clone

新对象 = clone 已有对象

 

例子:

 

class Student

{

       public $stu_id;

       public $stu_name;

 

       public function __construct($id,$name)

       {

              $this->stu_id= $id;

              $this->stu_name= $name;

       }

}

 

$stu1 = new Student(“100”,”song”);

$stu2 = clone $stu1;

echo $stu1->stu_name;

echo $stu2->stu_name;

$stu1->stu_name = “songyang”;

echo $stu1->stu_name;

echo $stu2->stu_name;

 

输出:song song  songyang song

 

 

常见的操作,再克隆对象时,需要对对象的某些特殊属性进行修改。意味着,需要做一些特殊的处理。

使用,在克隆时,自动调用的方法 __clone()来实现。

自动使用克隆出来的对象,来调用这个__clone()方法,意味着,该方法内部的$this,表示新对象。

 

例子:

 

class Student

{

       public $stu_id;

       public $stu_name;

 

       public function __construct($id,$name)

       {

              $this->stu_id= $id;

              $this->stu_name= $name;

       }

 

       public function __clone()

       {

              $this->stu_id= “0607002”;

       }

}

 

$stu1 = new Student(“1000”,”joker”);

$stu2 = clone $stu1;

echo $stu1->stu_id;

echo $stu2->stu_id;

 

输出:1000  0607002

 

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!