PHP Object-Oriented Programming - Basic Practice DAY 2

WBOY
Release: 2016-07-29 09:15:05
Original
890 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><div class=

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

?>
Output:
Jordan

Dribbling

Passing
Constructor
对象被实例化的时候自动调用
	function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了
		echo "In NbaPlayer Constructor\n";
		$this->name=$name; //$this是PHP里的伪变量,表示对象自身。可以通过$this->的方法访问对象的属性和方法
		$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";
	}
}

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

//每一次用new实例化对象的时候,都会用类名后面的参数列比调用构造函数
$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

对象被实例化的时候自动调用
	function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了
		echo "In NbaPlayer Constructor\n";
		$this->name=$name; //$this是PHP里的伪变量,表示对象自身。可以通过$this->的方法访问对象的属性和方法
		$this->height=$height;
		$this->weight=$weight;
		$this->team=$team;
		$this->playerNumber=$playerNumber; 
	}
	
	//析构函数,在程序执行结束的时候会自动调用
	//析构函数通常被用于清理程序使用的资源,比如程序使用了打印机,那么可以在析构函数里面释放打印机资源。
	function __destruct(){
		echo "Destroying ".$this->name."
"; } //定义方法 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"; } } //类到对象的实例化 //类的实例化为对象时使用关键字new,new之后紧跟这类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull","23"); //对象中的属性成员可以通过->符号来访问 echo $jordan->name."\n"; //对象中的成员方法可以通过->符号来访问 $jordan->dribble(); $jordan->pass(); //每一次用new实例化对象的时候,都会用类名后面的参数列比调用构造函数 $james= new NbaPlayer("James","203cm","120kg","Heat","6"); echo $james->name; //通过把变量设置为null,可以触发析构函数的调用 $james=null; echo "From now on James will not be used.
"; ?>
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><div class=

对象被实例化的时候自动调用
	function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了
		echo "In NbaPlayer Constructor\n";
		$this->name=$name; //$this是PHP里的伪变量,表示对象自身。可以通过$this->的方法访问对象的属性和方法
		$this->height=$height;
		$this->weight=$weight;
		$this->team=$team;
		$this->playerNumber=$playerNumber; 
	}
	
	//析构函数,在程序执行结束的时候会自动调用
	//析构函数通常被用于清理程序使用的资源,比如程序使用了打印机,那么可以在析构函数里面释放打印机资源。
	function __destruct(){
		echo "Destroying ".$this->name."
"; } //定义方法 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"; } } //类到对象的实例化 //类的实例化为对象时使用关键字new,new之后紧跟这类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull","23"); //对象中的属性成员可以通过->符号来访问 echo $jordan->name."\n"; //对象中的成员方法可以通过->符号来访问 $jordan->dribble(); $jordan->pass(); //每一次用new实例化对象的时候,都会用类名后面的参数列比调用构造函数 $james= new NbaPlayer("James","203cm","120kg","Heat","6"); echo $james->name; //通过把变量设置为null,可以触发析构函数的调用 //当对象不会再被使用的时候,会触发析构函数 //james1也是指向new NbaPlayer(); $james1=$james; $james=null; echo "From now on James will not be used.
"; ?>Output:In NbaPlayer ConstructorJordanDribblingPassing
In NbaPlayer Constructor

James
From now on James will not be used.
Destroying James
Destroying Jordan
Example two:
对象被实例化的时候自动调用
	function __construct($name,$height,$weight,$team,$playerNumber){ //没有被明确调用,但是也被调用了
		echo "In NbaPlayer Constructor\n";
		$this->name=$name; //$this是PHP里的伪变量,表示对象自身。可以通过$this->的方法访问对象的属性和方法
		$this->height=$height;
		$this->weight=$weight;
		$this->team=$team;
		$this->playerNumber=$playerNumber; 
	}
	
	//析构函数,在程序执行结束的时候会自动调用
	//析构函数通常被用于清理程序使用的资源,比如程序使用了打印机,那么可以在析构函数里面释放打印机资源。
	function __destruct(){
		echo "Destroying ".$this->name."
"; } //定义方法 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"; } } //类到对象的实例化 //类的实例化为对象时使用关键字new,new之后紧跟这类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull","23"); //对象中的属性成员可以通过->符号来访问 echo $jordan->name."\n"; //对象中的成员方法可以通过->符号来访问 $jordan->dribble(); $jordan->pass(); //每一次用new实例化对象的时候,都会用类名后面的参数列比调用构造函数 $james= new NbaPlayer("James","203cm","120kg","Heat","6"); echo $james->name; //通过把变量设置为null,可以触发析构函数的调用 //当对象不会再被使用的时候,会触发析构函数 //james1也是指向new NbaPlayer(); $james1=$james; //$james1直接指向詹姆斯 $james2=&james; //$james2相当于$james的影子,指向$james, $james再指向詹姆斯 $james=null; //不需要再次设置$james2=null,因为他俩的效果是一样的 $james1=null; //任何一个赋值为null相当于删除了一个对象的引用 echo "From now on James will not be used.
"; ?>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!