What are the characteristics of object-oriented php? Introduction to object-oriented features of PHP (code)

不言
Release: 2023-04-03 11:48:01
Original
2756 people have browsed it

The PHP object-oriented mentioned here should be called PHP object-oriented programming to be precise. The three basic characteristics of PHP object-oriented programming are encapsulation, inheritance and polymorphism. Let’s take a look at the object-oriented aspects of PHP separately. Three major characteristics.

1. Three major characteristics of PHP object-oriented:
1. Encapsulation:
Encapsulation is to encapsulate abstracted data and data operations, and the data is protected internally. Other parts of the program can only operate on the data through authorized operations (member methods).

Access control character assignment value.png

2. Inheritance:
The so-called inheritance is that a subclass extends the parent class to the parent class Some (public/protected) properties and (public/protected) methods are inherited. Private cannot be inherited.

  作用:增加代码复用性,管理代码成本低。
 
  基本语法:
  class 类名 extends 父类名 {
        //需要的非私有属性和方法
  }
Copy after login

Inheritance.png

<?php
    
    //父类
    Class Student {
        public $name;
        protected $age;
        protected $grade;

        // public __construct() {

        // }

        public function showInfo() {
            echo $this -> name."<br/>".$this -> age;
        }
    }

    /**
    * 小学生
    */
    class Pupil extends Student
    {
        public function test() {
            echo "<br/>小学生在考试。。。";
        }
    }

    /**
    * 大学生
    */
    class Graduate extends Student
    {
        public function test() {
            echo "<br/>大学生在考试。。。";
        } 
    }

    $stu1 = new Pupil();
    $stu1 -> name = "lizzy";
    $stu1 -> test();
    $stu1 -> showInfo();

    $stu2 = new Graduate();
    $stu2 -> name = "zxm";
    $stu2 -> test();
    $stu2 -> showInfo();
?>
Copy after login

Note:
(1) protected properties and methods are protected and cannot be called directly in subclasses. Method access defined in subclasses.

Class ParentClass {
    public $name = &#39;lizzy&#39;;
    protected $age = 24;
    private $sex = "女";

    public function test1() {
        echo "<br/>展示public方法";
    }

    protected function test2() {
        echo "<br/>展示protected方法";
    }

    private function test3() {
        echo "<br/>展示private方法";
    }
}

Class SubClass extends ParentClass {
    function show() {
        echo "age=".$this->age;
        // echo "sex=".$this->sex;//是不能继承下来的
        $this -> test2();
    }
}

$sub1 = new SubClass();
$sub1 -> show();
$sub1 -> test1();
Copy after login

(2) If you want the subclass to call the parent class's constructor or other methods (public/protected), you can use class name::method name; or parent::method name;

Class A {
    public $n1=90;

    public function __construct() {
        echo "A的构造方法";
    }
}

Class B extends A {
    function __construct() {
        echo "B的构造方法";
        //调用父类的两种方法
        // A::__construct();
        parent::__construct();
    }
}
$b = new B();
Copy after login

(3) When a subclass method is completely consistent with the parent class method (protected/public), we call it method overwriting (rewriting).

3. Polymorphism:
"Overloading" is a manifestation of class polymorphism;
The concept of overloading: the function name is the same, but the number or parameter types of the function are different. , to call the same function name, but different functions can be distinguished.

Magic function __call, but its use is not recommended; if an object calls a certain method, and the method does not exist, the system will automatically call __call.

<?php

    Class A {

        public function test1($p) {
            echo "接收一个参数<br/>";
        }

        public function test2($p) {
            echo "接收两个参数<br/>";
        }

        //提供__call 它一个对象调用某个方法,而该方法不存在,则系统会自动调用__call
        function __call($method,$p) {
            if ($method == "test") {
                if (count($p) == 1) {
                    $this -> test1($p);
                } elseif (count($p) == 2) {
                    $this -> test2($p);
                }
            }
        }
    }

    $a = new A();
    $a -> test(1);
    $a -> test(21,43)
?>
Copy after login

Common magic constants: two underscores LINE two underscores; the current number of lines;
two underscores FILE two underscores; the absolute path of the current file;

Rewriting of methods /Override:
1. When a parent class knows that all subclasses have a method, but the parent class is not sure how to write the method, it can let the subclass override this method.
Use as follows:

<?php

    Class Animal {
        function cry() {
            echo "不知道怎么叫";
        }
    }

    //重写
    Class Dog extends Animal {
        function cry() {
            echo "小狗汪汪叫";
        }
    }

    //重写
    Class Pig extends Animal {
        function cry() {
            echo "小猪哼哼叫";
        }
    }

    $dog = new Dog();
    $dog -> cry();
    $pig = new Pig();
    $pig -> cry();

?>
Copy after login

Details of method rewriting:
1. The function name and number of parameters of the rewritten subclass must be consistent with those of the parent class; but the names of the parameters are not required to be the same.
2. If the subclass wants to call a method of the parent class (public/protected), you can use parent::method name(); or parent class name::method name(); and you can pass parameters according to the situation.
3. When implementing method coverage, the access modifiers can be different, but they must satisfy the access scope of the subclass >= the access scope of the parent class.

Polymorphism is reflected in:
When the subclass does not override the method of the parent class, the subclass calls the parent class. When the subclass overrides the method of the parent class, it calls its own method. .

Related recommendations:

PHP zero-based introductory tutorial

Talk about my understanding of object-oriented in PHP

The above is the detailed content of What are the characteristics of object-oriented php? Introduction to object-oriented features of PHP (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
m php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template