PHP study notes - object-oriented_php basics
Object-oriented is a computer programming architecture that is more flexible and scalable than process-oriented editing. Here I would like to remind beginners: If you want to go further than others on the road of programming, you must master object-oriented programming technology.
public Public: this class, subclasses, and external objects can all call
protected: subclasses of this class can be executed, external objects cannot call
private: only This class can be executed, and neither subclasses nor external objects can be called
Three major characteristics of object-oriented programming
1) Closure
Closeness can also be called information hiding. It is to separate the use and implementation of a class, leaving only limited interfaces (methods) to connect with the outside. For developers who use this class, they only need to know how to use this class, and do not need to care about how this class is implemented. Doing so allows developers to better focus on other things, while also avoiding the inconvenience caused by interdependencies between programs.
2) Inheritance
Inheritance means that a derived class (subclass) automatically inherits the properties and methods in one or more base classes (parent classes), and can override or add new ones. properties and methods. Inheritance simplifies the creation of objects and classes and increases code reproducibility. Inheritance is divided into single inheritance and multiple inheritance. PHP supports single inheritance, that is to say, a subclass has and has only one parent class.
3) Polymorphism
Polymorphism refers to the fact that different objects of the same class can obtain different results using the same method. This technology is called polymorphism. Polymorphism enhances software flexibility and reusability.
Definition of class
A class can contain its own constants, variables (called "properties") and functions (called "methods").
Like many object-oriented languages, PHP also defines classes through the class keyword plus the class name. The format of the class is as follows:
Copy code The code is as follows:
<?php Class myobject{ //…… } ?>
Definition: Birds of a feather flock together, and objects with similar characteristics are grouped together Within a class, a class defines the same properties and methods that these similar objects have. A class is a description of similar objects, called the definition of the class, which is the blueprint or prototype of the object of that class.
The object of a class is called an instance of the class. To create an instance of a class, the new keyword must be used.
001ZpQGYty6MeYnSNUh25&690
##Copy code The code is as follows:
<?php //类的定义以关键字class开始,类的命名通常以每个单词第一个字母大写 class NbaPlayer{ public $name = "Jordan"; //定义属性 public $height = "198cm"; public $team = "Bull"; public $playerNumber = "23"; //定义方法 public function run(){ echo "Running\n"; } public function dribblr(){ echo "Dribbling\n"; } public function pass(){ echo "Passing\n"; } } //类到对象的实例化 //类的实例化为对象时使用关键字new,new之后紧跟类的名称和一对括号 $jordan = new NbaPlayer(); //对象中的属性成员可以通过"->"符号来访问 echo $jordan->name."\n"; //对象中的成员方法可以通过"->"符号来访问 $jordan->dribble(); $jordan->run(); ?>
Let’s expand the myobject class above and add a member method to it. The code is as follows:
Copy code The code is as follows:
<?php classmyobject{ function getobjectname($name){ echo "商品名称为:".$name; } } ?>
A class is an abstract description, a collection of objects with similar functions. If you want to use the methods and variables in the class, you must first implement them into an entity, that is, an object.
Since there are variables, of course there will also be constants. A constant is a quantity that does not change and is a constant value. A well-known constant is pi. To define constants, use the keyword const, such as:
ConstPI=3.14159;
Copy code The code is as follows:
<?php //类的定义以关键字class开始,类的命名通常以每个单词第一个字母大写 class NbaPlayer{ public $name = "Jordan"; //定义属性 public $height = "198cm"; public $team = "Bull"; public $playerNumber = "23"; //构造函数,在对象被实例化的时候自动调用 function __construct($name,$height,$weight,$team){ echo "It is an NbaPlayer constructor\n"; $this->name = $name; //$this是PHP里面的伪变量,表示对象自身。可以通过$this->的方式访问对象的属性和方法 $this->height = $height; $this->weight = $weight; $this->team = $team; } //定义方法 public function run(){ echo "Running\n"; } public function dribblr(){ echo "Dribbling\n"; } public function pass(){ echo "Passing\n"; } } //类到对象的实例化 //类的实例化为对象时使用关键字new,new之后紧跟类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull"); //对象中的属性成员可以通过"->"符号来访问 echo $jordan->name."\n"; //对象中的成员方法可以通过"->"符号来访问 $jordan->dribble(); $jordan->run(); //每一次用new实例化对象的时候,都会用类名后面的参数列表调用构造函数 $james = new NbaPlayer("James","203cm","120kg","Heat") echo $james->name."\n"; ?>
Copy code The code is as follows:
<?php //类的定义以关键字class开始,类的命名通常以每个单词第一个字母大写 class NbaPlayer{ public $name = "Jordan"; //定义属性 public $height = "198cm"; public $team = "Bull"; public $playerNumber = "23"; //构造函数,在对象被实例化的时候自动调用 function __construct($name,$height,$weight,$team){ echo "It is an NbaPlayer constructor\n"; $this->name = $name; //$this是PHP里面的伪变量,表示对象自身。可以通过$this->的方式访问对象的属性和方法 $this->height = $height; $this->weight = $weight; $this->team = $team; } //析构函数,在程序执行结束的时候会自动调用 //析构函数通常被用于清理程序使用的资源。比如程序使用了打印机,那么可以再析构函数里面释放打印机资源 function __destruct(){ echo "Destroying".$this->name."\n"; } //定义方法 public function run(){ echo "Running\n"; } public function dribblr(){ echo "Dribbling\n"; } public function pass(){ echo "Passing\n"; } } //类到对象的实例化 //类的实例化为对象时使用关键字new,new之后紧跟类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull"); //对象中的属性成员可以通过"->"符号来访问 echo $jordan->name."\n"; //对象中的成员方法可以通过"->"符号来访问 $jordan->dribble(); $jordan->run(); //每一次用new实例化对象的时候,都会用类名后面的参数列表调用构造函数 $james = new NbaPlayer("James","203cm","120kg","Heat") echo $james->name."\n"; //通过把变量设为null,可以触发析构函数的调用 //当对象不再使用的时候会触发析构函数 $james = null; echo "from now on James will not be used.\n" ?>
Copy code The code is as follows:
<?php //类的定义以关键字class开始,类的命名通常以每个单词第一个字母大写 class NbaPlayer{ public $name = "Jordan"; //定义属性 public $height = "198cm"; public $team = "Bull"; public $playerNumber = "23"; //构造函数,在对象被实例化的时候自动调用 function __construct($name,$height,$weight,$team){ echo "It is an NbaPlayer constructor\n"; $this->name = $name; //$this是PHP里面的伪变量,表示对象自身。可以通过$this->的方式访问对象的属性和方法 $this->height = $height; $this->weight = $weight; $this->team = $team; } //析构函数,在程序执行结束的时候会自动调用 //析构函数通常被用于清理程序使用的资源。比如程序使用了打印机,那么可以再析构函数里面释放打印机资源 function __destruct(){ echo "Destroying".$this->name."\n"; } //定义方法 public function run(){ echo "Running\n"; } public function dribblr(){ echo "Dribbling\n"; } public function pass(){ echo "Passing\n"; } } //类到对象的实例化 //类的实例化为对象时使用关键字new,new之后紧跟类的名称和一对括号 $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull"); //对象中的属性成员可以通过"->"符号来访问 echo $jordan->name."\n"; //对象中的成员方法可以通过"->"符号来访问 $jordan->dribble(); $jordan->run(); //每一次用new实例化对象的时候,都会用类名后面的参数列表调用构造函数 $james = new NbaPlayer("James","203cm","120kg","Heat") echo $james->name."\n"; //对象的引用用于访问对象的属性和方法,$james,$james1和$james2都是对象的引用 //$james和$james1是对象的两个独立引用 //$james2是$james的影子,使用对象的同一个引用,任何一个赋值为null相当于删除了同一个引用 $james1 = $james; $james2 = &$james $james = null; echo "from now on James will not be used.\n" ?>
PHP object-oriented inheritance constructor
PHP object-oriented this binding detailed explanation
PHP object-oriented use of some keywords
The above is the detailed content of PHP study notes - object-oriented_php basics. For more information, please follow other related articles on the PHP Chinese website!

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

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











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
