Object Oriented php Study Notes

  • 2021-07-26 07:12:41
  • OfStack

public Public: This class, subclass and external object can be called
protected Protected: Subclass of this class, can be executed, can not be called by external objects
private Private: It can only be executed by this class, and neither subclasses nor external objects can be called
Three Characteristics of Object-Oriented Programming

1) Closure

Closure can also be called information hiding. Is to separate the use and implementation of a class, leaving only a limited interface (method) and external contact. For developers who use this class, it is only necessary to know how to use this class, and not to care about how this class is implemented. Doing so allows developers to better focus on other things, while avoiding the inconvenience of interdependence between programs.

2) Inheritance

Inheritance means that a derived class (child class) automatically inherits properties and methods from one or more base classes (parent classes), and can override or add new properties and methods. Inheritance simplifies the creation of objects and classes and increases the repeatability of code. Inheritance is divided into single inheritance and multiple inheritance. PHP supports single inheritance, that is, one subclass has only one parent class.

3) Polymorphism

Polymorphism refers to different objects of the same class, and different results can be obtained by using the same method. This technology is called polymorphism. Polymorphism enhances the flexibility and reusability of software.

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 class keywords plus class names. Class has the following format:


<?php
  Class myobject{
    // ...
}
?>

Definition: Birds of a feather flock together. Objects with similar characteristics are grouped into a class, which defines the same attributes and methods that these similar objects have. A class is a description of similar objects, called a class definition, and is the blueprint or prototype of such objects.

The object of the class is called an instance of the class. To create an instance of a class, you must use the new keyword.
001ZpQGYty6MeYnSNUh25 & 690


<?php
// The definition of the class is based on the keyword class Beginning, classes are usually named with each word first 1 Capitalized letters
    class NbaPlayer{
        public $name = "Jordan"; // Defining Attributes
        public $height = "198cm";
        public $team = "Bull";
        public $playerNumber = "23";         // Definition method
    public function run(){
        echo "Running\n";
    }
    public function dribblr(){
        echo "Dribbling\n";
    }
    public function pass(){
        echo "Passing\n";
    }
}
    // Instantiation of class to object
    // Use keywords when instantiating classes into objects new , new Followed by the name of the class and 1 Parentheses
    $jordan = new NbaPlayer();      // Property members in the object can be accessed through the "->" Symbol to access
    echo $jordan->name."\n";     // Member methods in the object can be used through the "->" Symbol to access
    $jordan->dribble();
    $jordan->run();
?>

Member method

A function in a class is called a member method. The only difference between a function and a member method is that a function implements an independent function, while a member method implements a behavior in a class and is a part of a class.
Let's extend the above myobject class and add a member method to it. The code is as follows:


<?php
classmyobject{
   function getobjectname($name){
      echo " The commodity name is: ".$name;
   }
}
?>

The function of this method is to output the commodity name, which is passed in through the parameters of the method.
Class is an abstract description and a collection of 1 group of objects with similar functions. If you want to use the methods and variables in a class, you must first implement it to an entity, that is, an object.

Class constant
Since there are variables, of course there will be constants. A constant is a constant that does not change. A well-known constant is pi Pi. Defining constants uses 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 constructors call this method each time a new object is created, so it's a good idea to do a little initialization before using the object.


<?php
// The definition of the class is based on the keyword class Beginning, classes are usually named with each word first 1 Capitalized letters
    class NbaPlayer{
        public $name = "Jordan"; // Defining Attributes
        public $height = "198cm";
        public $team = "Bull";
        public $playerNumber = "23";         // Constructor, which is called automatically when the object is instantiated
        function __construct($name,$height,$weight,$team){
            echo "It is an  NbaPlayer constructor\n";
            $this->name = $name;
            //$this Yes PHP Pseudo variables inside represent the object itself. It can be passed through $this-> To access the properties and methods of an object in the way of
            $this->height = $height;
            $this->weight = $weight;
            $this->team = $team;
        }         // Definition method
    public function run(){
        echo "Running\n";
    }
    public function dribblr(){
        echo "Dribbling\n";
    }
    public function pass(){
        echo "Passing\n";
    }
}
    // Instantiation of class to object
    // Use keywords when instantiating classes into objects new , new Followed by the name of the class and 1 Parentheses
    $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");        // Property members in the object can be accessed through the "->" Symbol to access
    echo $jordan->name."\n";     // Member methods in the object can be used through the "->" Symbol to access
    $jordan->dribble();
    $jordan->run();     // Every 1 Secondary use new When an object is instantiated, the constructor is called with the argument list after the class name
    $james = new NbaPlayer("James","203cm","120kg","Heat")
    echo $james->name."\n";
?>

Destructor


<?php
// The definition of the class is based on the keyword class Beginning, classes are usually named with each word first 1 Capitalized letters
    class NbaPlayer{
        public $name = "Jordan"; // Defining Attributes
        public $height = "198cm";
        public $team = "Bull";
        public $playerNumber = "23";
       
        // Constructor, which is called automatically when the object is instantiated
        function __construct($name,$height,$weight,$team){
            echo "It is an  NbaPlayer constructor\n";
            $this->name = $name;
            //$this Yes PHP Pseudo variables inside represent the object itself. It can be passed through $this-> To access the properties and methods of an object in the way of
            $this->height = $height;
            $this->weight = $weight;
            $this->team = $team;
        }
       
        // Destructor, which is called automatically at the end of program execution
        // Destructors are usually used to clean up resources used by programs. For example, if the program uses a printer, you can release the printer resources in the destructor
        function __destruct(){
            echo "Destroying".$this->name."\n";
        }
       
        // Definition method
    public function run(){
        echo "Running\n";
    }
    public function dribblr(){
        echo "Dribbling\n";
    }
    public function pass(){
        echo "Passing\n";
    }
}
    // Instantiation of class to object
    // Use keywords when instantiating classes into objects new , new Followed by the name of the class and 1 Parentheses
    $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");   
   
    // Property members in the object can be accessed through the "->" Symbol to access
    echo $jordan->name."\n";
   
    // Member methods in the object can be used through the "->" Symbol to access
    $jordan->dribble();
    $jordan->run();
   
    // Every 1 Secondary use new When an object is instantiated, the constructor is called with the argument list after the class name
    $james = new NbaPlayer("James","203cm","120kg","Heat")
    echo $james->name."\n";
   
    // By setting the variable to null That triggers a call to the destructor
    // Triggers the destructor when the object is no longer in use
    $james = null;
    echo "from now on James will not be used.\n"
?>

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

Reference to the object


<?php
// The definition of the class is based on the keyword class Beginning, classes are usually named with each word first 1 Capitalized letters
    class NbaPlayer{
        public $name = "Jordan"; // Defining Attributes
        public $height = "198cm";
        public $team = "Bull";
        public $playerNumber = "23";         // Constructor, which is called automatically when the object is instantiated
        function __construct($name,$height,$weight,$team){
            echo "It is an  NbaPlayer constructor\n";
            $this->name = $name;
            //$this Yes PHP Pseudo variables inside represent the object itself. It can be passed through $this-> To access the properties and methods of an object in the way of
            $this->height = $height;
            $this->weight = $weight;
            $this->team = $team;
        }         // Destructor, which is called automatically at the end of program execution
        // Destructors are usually used to clean up resources used by programs. For example, if the program uses a printer, you can release the printer resources in the destructor
        function __destruct(){
            echo "Destroying".$this->name."\n";
        }         // Definition method
    public function run(){
        echo "Running\n";
    }
    public function dribblr(){
        echo "Dribbling\n";
    }
    public function pass(){
        echo "Passing\n";
    }
}
    // Instantiation of class to object
    // Use keywords when instantiating classes into objects new , new Followed by the name of the class and 1 Parentheses
    $jordan = new NbaPlayer("Jordan","198cm","98kg","Bull");        // Property members in the object can be accessed through the "->" Symbol to access
    echo $jordan->name."\n";     // Member methods in the object can be used through the "->" Symbol to access
    $jordan->dribble();
    $jordan->run();     // Every 1 Secondary use new When an object is instantiated, the constructor is called with the argument list after the class name
    $james = new NbaPlayer("James","203cm","120kg","Heat")
    echo $james->name."\n";     // Object is used to access the object's properties and methods, $james,$james1 And $james2 Are references to objects
    //$james And $james1 Are two independent references to an object
    //$james2 Yes $james The shadow of the object, using the same as the object 1 References, any 1 A value assigned to null Equivalent to deleting the same 1 References
    $james1 = $james;
    $james2 = &$james     $james = null;
    echo "from now on James will not be used.\n"
?>


Related articles: