Home > Backend Development > PHP Tutorial > PHP Object-Oriented Programming - Basic Practice DAY 2

PHP Object-Oriented Programming - Basic Practice DAY 2

WBOY
Release: 2016-07-29 09:15:05
Original
1028 people have browsed it

Object-oriented practices in PHP Basic practices
Advanced practices
Special practices
Concept of class
Concept of instantiation
Constructor
Destructor
Data access

Object Concept of reference Concept of class
PHP Object Oriented <br> Programming - Basic Practice DAY 2PHP面向<strong>Likes</strong>? Birds of a feather flock together and put together those with similar characteristics <br> objects <br> are classified into a class <strong>? A class defines the same properties and methods </strong> that these similar <br> objects <strong> have. A class is a description of similar </strong> objects <br>, called the definition of the class, which is the <strong> object </strong> of that class. The blueprint or prototype<strong>? The </strong>object<br> of the class is called an instance of the class //The class draws a frame, and the <strong>object</strong>fills the frame<strong>? The attributes and methods of the class are collectively called class members</strong>Example<br>? NBA player is the definition of a class (Class Definition)<br>? Jordan, James, and Kobe are called instances of the class (Instance)<br>NBA player<br>+name<br>+height<br>+weight<br>+team<br>+player number<br>-- ----------<br>+Running()<br>+Jump()<br>+Dribbling()<br>+Shoot()<br>+Dunk()<br>+Pass()<br>Instantiate of class <br><br><Insert two pictures><br><p><img src=

PHP面向<strong><img src=

PHP面向<strong>Case</strong>Class and class instantiation case<br>? How to define a class</p>? How to instantiate an <br>object of a class<br><br>? How to call a class method<br>? Constructor<strong></strong>? Destructor<br><pre name=<?php //类的定义以关键字class开始,后面跟着这个类的名称。类的命名通常每个单词的第一个字母大写。以中括号开始和结束 //定义NbaPlayer类 class NbaPlayer{ //定义属性 public $name="Jordan"; public $height="198cm"; public $weight="98kg"; public $team="Bull"; public $playerNumber="23"; //定义方法 public function run(){ echo "Running\n"; } public function jump(){ echo "Jumping\n"; } public function dribble(){ echo "Dribbling\n"; } public function shoot(){ echo "Shooting\n"; } public function dunk(){ echo "Dunking\n"; } public function pass(){ echo "Passing\n"; } } //类到<strong>对象</strong>的实例化 //类的实例化为<strong>对象</strong>时使用关键字new,new之后紧跟这类的名称和一对括号 $jordan = new NbaPlayer(); //<strong>对象</strong>中的属性成员可以通过->符号来访问 echo $jordan->name."\n"; //<strong>对象</strong>中的成员方法可以通过->符号来访问 $jordan->dribble(); $jordan->pass(); ?><br>Output:<br>Jordan </p> <p>Dribbling </p>Passing <br>Constructor <div class="code" style="position:relative; padding:0px; margin:0px;"><pre name="code"><?php date_default_timezone_set("PRC"); //类的定义以关键字class开始,后面跟着这个类的名称。类的命名通常每个单词的第一个字母大写。以中括号开始和结束 //定义NbaPlayer类 class NbaPlayer{ //定义属性 public $name="Jordan"; public $height="198cm"; public $weight="98kg"; public $team="Bull"; public $playerNumber="23"; //构造函数,在<strong>对象</strong>被实例化的时候自动调用 function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了 echo "In NbaPlayer Constructor\n"; $this->name=$name; //$this是PHP里的伪变量,表示<strong>对象</strong>自身。可以通过$this->的方法访问<strong>对象</strong>的属性和方法 $this->height=$height; $this->weight=$weight; $this->team=$team; $this->playerNumber=$playerNumber; } //定义方法 public function run(){ echo "Running\n"; } public function jump(){ echo "Jumping\n"; } public function dribble(){ echo "Dribbling\n"; } public function shoot(){ echo "Shooting\n"; } public function dunk(){ echo "Dunking\n"; } public function pass(){ echo "Passing\n"; } } //类到<strong>对象</strong>的实例化 //类的实例化为<strong>对象</strong>时使用关键字new,new之后紧跟这类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull","23"); //<strong>对象</strong>中的属性成员可以通过->符号来访问 echo $jordan->name."\n"; //<strong>对象</strong>中的成员方法可以通过->符号来访问 $jordan->dribble(); $jordan->pass(); //每一次用new实例化<strong>对象</strong>的时候,都会用类名后面的参数列比调用构造函数 $james= new NbaPlayer("James","203cm","120kg","Heat","6"); echo $james->name; echo $james->height; echo $james->weight; echo $james->team; echo $james->playerNumber; ?>

Copy after login


Output:
In NbaPlayer Constructor
Jordan
Dribbling

Passing
In NbaPlayer Constructor
James
203cm
120kg
Heat
6

Destructor

<?php 
date_default_timezone_set("PRC");

//类的定义以关键字class开始,后面跟着这个类的名称。类的命名通常每个单词的第一个字母大写。以中括号开始和结束
//定义NbaPlayer类
class NbaPlayer{
	
	//定义属性
	public $name="Jordan"; 
	public $height="198cm";
	public $weight="98kg";
	public $team="Bull";
	public $playerNumber="23";

	//构造函数,在<strong>对象</strong>被实例化的时候自动调用
	function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了
		echo "In NbaPlayer Constructor\n";
		$this->name=$name; //$this是PHP里的伪变量,表示<strong>对象</strong>自身。可以通过$this->的方法访问<strong>对象</strong>的属性和方法
		$this->height=$height;
		$this->weight=$weight;
		$this->team=$team;
		$this->playerNumber=$playerNumber; 
	}
	
	//析构函数,在程序执行结束的时候会自动调用
	//析构函数通常被用于清理程序使用的资源,比如程序使用了打印机,那么可以在析构函数里面释放打印机资源。
	function __destruct(){
		echo "Destroying ".$this->name."<br/>";
	}

	//定义方法
	public function run(){
		echo "Running\n";
	}

	public function jump(){
		echo "Jumping\n";
	}

	public function dribble(){
		echo "Dribbling\n";
	}
	
	public function shoot(){
		echo "Shooting\n";
	}
	
	public function dunk(){
		echo "Dunking\n";
	}
	
	public function pass(){
		echo "Passing\n";
	}
}

//类到<strong>对象</strong>的实例化
//类的实例化为<strong>对象</strong>时使用关键字new,new之后紧跟这类的名称和一对括号
$jordan = new NbaPlayer("Jordan","198cm","98kg","Bull","23");
//<strong>对象</strong>中的属性成员可以通过->符号来访问
echo $jordan->name."\n";
//<strong>对象</strong>中的成员方法可以通过->符号来访问
$jordan->dribble();
$jordan->pass();

//每一次用new实例化<strong>对象</strong>的时候,都会用类名后面的参数列比调用构造函数
$james= new NbaPlayer("James","203cm","120kg","Heat","6");
echo $james->name;

//通过把变量设置为null,可以触发析构函数的调用
$james=null;
echo "From now on James will not be used. <br/>";
?>
Copy after login

Output:In NbaPlayer Constructor

Jordan

Dribbling
Passing
In NbaPlayer Constructor
James
Destroying James
From now on James will not be used.
Destroying Jordan


Object
Basic concept of reference

ObjectReference assignment ://image.codes51.com/Article/image/20150919/20150919094753_5210.jpg" alt="PHP Object-oriented
Programming - Basic Practice DAY 2">James is an object
. PHP面向<strong>$james is a reference to the </strong>object<br>, directly pointing to the <strong>object</strong> James. <br>$james1 and $james are two independent references. The reference of the <strong>object</strong>$james1 directly points to the <strong>object</strong> of James. <br>$james2 is a <strong>object</strong>reference that points to the <strong>object</strong>$james (a bit of a mouthful), and does not directly point to the <br>object<strong> of James. $james2 points to the </strong>object <strong> of James through the </strong>object<strong>reference of $james. Now it directly points to the </strong>object<strong>James' </strong>object<strong>. There are two references, namely $james and $james1. And $james2 is the image of $james. </strong>Example one:<br><pre name=<?php date_default_timezone_set("PRC"); //类的定义以关键字class开始,后面跟着这个类的名称。类的命名通常每个单词的第一个字母大写。以中括号开始和结束 //定义NbaPlayer类 class NbaPlayer{ //定义属性 public $name="Jordan"; public $height="198cm"; public $weight="98kg"; public $team="Bull"; public $playerNumber="23"; //构造函数,在<strong>对象</strong>被实例化的时候自动调用 function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了 echo "In NbaPlayer Constructor\n"; $this->name=$name; //$this是PHP里的伪变量,表示<strong>对象</strong>自身。可以通过$this->的方法访问<strong>对象</strong>的属性和方法 $this->height=$height; $this->weight=$weight; $this->team=$team; $this->playerNumber=$playerNumber; } //析构函数,在程序执行结束的时候会自动调用 //析构函数通常被用于清理程序使用的资源,比如程序使用了打印机,那么可以在析构函数里面释放打印机资源。 function __destruct(){ echo "Destroying ".$this->name."<br/>"; } //定义方法 public function run(){ echo "Running\n"; } public function jump(){ echo "Jumping\n"; } public function dribble(){ echo "Dribbling\n"; } public function shoot(){ echo "Shooting\n"; } public function dunk(){ echo "Dunking\n"; } public function pass(){ echo "Passing\n"; } } //类到<strong>对象</strong>的实例化 //类的实例化为<strong>对象</strong>时使用关键字new,new之后紧跟这类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull","23"); //<strong>对象</strong>中的属性成员可以通过->符号来访问 echo $jordan->name."\n"; //<strong>对象</strong>中的成员方法可以通过->符号来访问 $jordan->dribble(); $jordan->pass(); //每一次用new实例化<strong>对象</strong>的时候,都会用类名后面的参数列比调用构造函数 $james= new NbaPlayer("James","203cm","120kg","Heat","6"); echo $james->name; //通过把变量设置为null,可以触发析构函数的调用 //当<strong>对象</strong>不会再被使用的时候,会触发析构函数 //james1也是指向new NbaPlayer(); $james1=$james; $james=null; echo "From now on James will not be used. <br/>"; ?>Output:<strong>In NbaPlayer Constructor</strong>Jordan<strong>Dribbling</strong>Passing<br>In NbaPlayer Constructor</p>James<br>From now on James will not be used.<br>Destroying James<br>Destroying Jordan<br>Example two:<br><div class="code" style="position:relative; padding:0px; margin:0px;"><pre name="code"><?php date_default_timezone_set("PRC"); //类的定义以关键字class开始,后面跟着这个类的名称。类的命名通常每个单词的第一个字母大写。以中括号开始和结束 //定义NbaPlayer类 class NbaPlayer{ //定义属性 public $name="Jordan"; public $height="198cm"; public $weight="98kg"; public $team="Bull"; public $playerNumber="23"; //构造函数,在<strong>对象</strong>被实例化的时候自动调用 function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了 echo "In NbaPlayer Constructor\n"; $this->name=$name; //$this是PHP里的伪变量,表示<strong>对象</strong>自身。可以通过$this->的方法访问<strong>对象</strong>的属性和方法 $this->height=$height; $this->weight=$weight; $this->team=$team; $this->playerNumber=$playerNumber; } //析构函数,在程序执行结束的时候会自动调用 //析构函数通常被用于清理程序使用的资源,比如程序使用了打印机,那么可以在析构函数里面释放打印机资源。 function __destruct(){ echo "Destroying ".$this->name."<br/>"; } //定义方法 public function run(){ echo "Running\n"; } public function jump(){ echo "Jumping\n"; } public function dribble(){ echo "Dribbling\n"; } public function shoot(){ echo "Shooting\n"; } public function dunk(){ echo "Dunking\n"; } public function pass(){ echo "Passing\n"; } } //类到<strong>对象</strong>的实例化 //类的实例化为<strong>对象</strong>时使用关键字new,new之后紧跟这类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull","23"); //<strong>对象</strong>中的属性成员可以通过->符号来访问 echo $jordan->name."\n"; //<strong>对象</strong>中的成员方法可以通过->符号来访问 $jordan->dribble(); $jordan->pass(); //每一次用new实例化<strong>对象</strong>的时候,都会用类名后面的参数列比调用构造函数 $james= new NbaPlayer("James","203cm","120kg","Heat","6"); echo $james->name; //通过把变量设置为null,可以触发析构函数的调用 //当<strong>对象</strong>不会再被使用的时候,会触发析构函数 //james1也是指向new NbaPlayer(); $james1=$james; //$james1直接指向詹姆斯 $james2=&james; //$james2相当于$james的影子,指向$james, $james再指向詹姆斯 $james=null; //不需要再次设置$james2=null,因为他俩的效果是一样的 $james1=null; //任何一个赋值为null相当于删除了一个<strong>对象</strong>的引用 echo "From now on James will not be used. <br/>"; ?>

Copy after login

Output:
In NbaPlayer Constructor
Jordan
Dribbling
Passing
In NbaPlayer Constructor
James
Destroying James
From now on James will not be used.
Destroying Jordan

The above introduces PHP object-oriented programming - basic practice DAY 2, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Previous article:PHP multi-dimensional array deduplication (deduplication for any key value) - uniqueness of two-dimensional array - time complexity ~On Next article:PHP parameter passing method 1--ajax
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template