Object-oriented practice in PHP - basic practice
(1) Basic practice
1. The concept of class
1. Common points:
all have the same attributes
all There are the same methods
2. Differences:
The specific values are different
3. Summary: They are all Objects of a class
Speaking of classes, let’s talk about the concept of classes
1) Class definition
-We say that things flock together and people divide into groups. Classify objects with similar characteristics into a class
So when we look at objects, try to compare their properties and methods. If their properties and methods are very similar, we can put They are classified into one class
-The class defines that these similar objects have the same properties and methods
For example: an NBA player is the definition of a class, which stipulates that as long as it is an NBA Players must have: age, name, height, weight, team, player number
and then they will also have the same skills:
But the question is, is there a specific height defined in this class, is there a specific name, is there a specific weight, none of them, but if we write all these attributes, will it become a specific one? Object
This is the relationship between classes and objects. Specifically, a class is a description of similar objects. This is called the definition of a class. A class is the blueprint or prototype of all these similar objects
For example, how did the object James come from? Just set the attribute value of this class.
The object of the class is an <strong>instance(Instance)</strong>
of the class. This operation is also called instantiation of a class.
For example, James, Jordan, and Kobe are all instances of the NBA player class. The process of instantiating them is called instantiation of a class.
For classes, we can The understanding is that it is just an empty shelf. It only defines how objects are divided. As for objects, it is defined. For specific values, all attribute values must be written in detail.
Supplement A concept: We collectively refer to the objects and methods of a class as class members
2. The concept of instantiation
1) Definition of instantiation of a class
is to create a specific object through a class. This process is the instantiation of the class, which is equivalent to filling this empty shelf.
Example: For example, through the NBA player class we created James and Kobe Bryant , Jordan, among which the NBA player class was instantiated three times
Summary: There is only one class, but it can be instantiated multiple times to obtain multiple specific Object
Tip: How to implement it, we will demonstrate it in the code later
2) Instantiation of the class
2) Example: Take Jordan as an example
The left side is a class with no data, and the right side is Jordan. So how to instantiate it is to directly add these names, height, weight, team, and players If the values of the numbers are filled in specifically, then the Jordan object will be instantiated
Tip: Instantiation can be understood as making the class into a practical example
3. Specific cases
1) Case study objectives
1. How to define a class
2. How to instantiate a class
3. How to call a class Properties and methods
2) See the following code for details
<?php /*** * 案例目标: * 1.类的定义 * 2.类的实例化 * 3.属性的赋值 * 4.方法的调用 * */ /***定义一个类,类的名字叫NbaPlayer(中文=NBA球员) * 提示: * 1.定义类的过程:我们首先以class开始,然后写上类 名称:NbaPlayer,最后要写上一对{} * 2.类的名字通常首字母要大写 * 3 * ***/ class NbaPlayer{ //定义属性 public $name = ""; public $height = ""; public $weight = ""; public $team = ""; public $playerName = ""; //定义方法 //定义跑的方法 //提示: //方法定义的过程: //1.写上方法的类型,public,表示公共的方法,可以被外部直接调用 //2.写上function //3.定义方法的名称,然后写上一对(),最后{}结尾 //总结: //方法定义和之前js中函数的定义是一样的,只是类中的方法多了一个public public function run(){ //里面的代码我们称之为业务逻辑 echo "跑步<br/>"; } //定义跳跃方法 public function jump(){ echo "跳跃<br/>"; } //定义运球方法 public function dribble(){ echo "运球<br/>"; } //定义投篮的方法 public function shoot(){ echo "投篮<br/>"; } //定义扣篮方法 public function dunk(){ echo "扣篮<br/>"; } //定义传球 public function pass(){ echo "传球<br/>"; } } //类到对象的实例化 /*总结: 1、类的实例化过程= 通过new 类名() 即可完成一个类的实例化过程 */ //1.创建乔丹 $jordan = new NbaPlayer();//类的实例化 //类赋值 $jordan->name = "乔丹"; $jordan->height = "1.98米"; $jordan->weight = "98公斤"; $jordan->team = "公牛"; $jordan->playerName = "23"; //输出对象值 print_r("乔丹名称:".$jordan->name."<br/>"); print_r("乔丹身高:".$jordan->height."<br/>"); //总结:通过->可以调用对象里的属性 echo "<br/>"; //输出对象方法 $jordan->dribble(); $jordan->pass(); //总结:通过->符号可以调用对象的方法 ?>
The above is the detailed content of Object-oriented practice in PHP - basic practice cases. For more information, please follow other related articles on the PHP Chinese website!

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita


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

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
