Home Backend Development PHP Tutorial PHP study notes - object-oriented_php basics

PHP study notes - object-oriented_php basics

Apr 02, 2018 am 11:08 AM
php Base object

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{
    //……
}
?>
Copy after login

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();
?>
Copy after login

Member method

The function in the class is called member methods. The only difference between functions and member methods is that functions implement an independent function, while member methods implement a behavior in the class and are part of the class.

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;
   }
}
?>
Copy after login

The function of this method is to output the product name, which is passed in through the parameters of the method.

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.

Class constants

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;

Constructor

PHP 5 allows developers to define a method as a constructor in a class. Classes with a constructor will call this method every time a new object is created, so it is very suitable for doing some initialization work before using the object.


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

Destructor


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

The destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed.

Object reference


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"
?>
Copy after login
Related recommendations:

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!

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

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: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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.

See all articles