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

PHP Object-Oriented Programming - Basic Practice DAY 2

Jul 29, 2016 am 09:15 AM
echo function public quot

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.


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What is the difference between the developer version and the public version of iOS? What is the difference between the developer version and the public version of iOS? Mar 01, 2024 pm 12:55 PM

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Implementing distributed task scheduling using Golang's web framework Echo framework Implementing distributed task scheduling using Golang's web framework Echo framework Jun 24, 2023 am 11:49 AM

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

See all articles