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>时使用关键字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 <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>对象被实例化的时候自动调用
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>对象被实例化的时候自动调用 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>对象被实例化的时候自动调用
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><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>对象被实例化的时候自动调用
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.

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools
