PHP Object-Oriented Programming - Basic Practice DAY 2
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
//类的定义以关键字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;
?>
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/>"; ?>
Output:In NbaPlayer Constructor
Jordan
DribblingPassing
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
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/>";
?>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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

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

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
