SoFunction
Updated on 2025-03-10

Detailed explanation of PHP object-oriented (III)

Object-Oriented The object concept is at the heart of object-oriented technology. In the world of display, we are all facing objects, such as computers, televisions, bicycles, etc. In object-oriented programming, an object is a whole composed of information and descriptions of processing information, and is an abstraction of the real world.

The three main characteristics of an object

Object's behavior: Those operations can be applied to the object, turning on the light and turning off the light are behaviors.
Object Shape: When applying those methods are how the object responds, color, size, and shape.
Object representation: The object representation is equivalent to an ID card, and the specific differences are distinguished between the same behavior and the state.

Object-oriented model

Object-oriented concept:

oop (object-oriented programming) It can make its code more concise and easy to maintain and have stronger reproducibility

1. PHP object-oriented (three)

4. Advanced practice of OOP

4.3 Static-static member

<?php
date_default_timezone_set("PRC");
/**
  * 1. The definition of the class starts with the class keyword, followed by the name of the class.  Class name naming is usually capitalized in the first letter of each word.
  * 2. Define the properties of the class
  * 3. Methods for defining classes
  * 4. Instantiate the object of the class
  * 5. Use the properties and methods of the object
  */
class Human{
 public $name;
 public $height;
 public $weight;
public function eat($food){
 echo $this->name."'s eating ".$food."<br/>";
 }
}
class Animal{
 public $kind;
 public $gender;
}
class NbaPlayer extends Human{
 // Definition of attributes of classpublic $name="Jordan";// Define attributespublic $height="198cm";
 public $weight="98kg";
 public $team="Bull";
 public $playerNumber="23";
 private $age="44";
 public $president="David Stern";
 // Definition of class methodpublic function changePresident($newP){
 $this->president=$newP;
 }
 public function run() {
 echo "Running<br/>";
 }
public function jump(){
 echo "Jumping<br/>";
 }
 public function dribble(){
 echo "Dribbling<br/>";
 }
 public function shoot(){
 echo "Shooting<br/>";
 }
 public function dunk(){
 echo "Dunking<br/>";
 }
 public function pass(){
 echo "Passing<br/>";
 }
 public function getAge(){
 echo $this->name."'s age is ".$this->age;
 }
 function __construct($name, $height, $weight, $team, $playerNumber){
 print $name . ";" . $height . ";" . $weight . ";" . $team . ";" . $playerNumber."\n";
 $this->name = $name; // $this is a pseudo-variable in php, indicating the object itself$this->height = $height; // The property value of the object can be set through $this$this->weight = $weight;
 $this->team = $team;
 $this->playerNumber = $playerNumber;
 }
}
/**
  * 1. Use the new keyword when instantiating a class as an object, followed by the name of the class and a pair of brackets.
  * 2. Use objects to assign values ​​like other values
  */
$jordan = new NbaPlayer("Jordan", "198cm","98kg","Bull","23");echo "<br/>";
$james=new NbaPlayer("James", "203cm", "120kg", "Heat", "6");echo "<br/>";
// The syntax used to access the object's properties is -> symbol, followed by the name of the propertyecho $jordan->name."<br/>";
// The syntax used by a method that calls an object is -> symbol, followed by the name of the method and a pair of brackets$jordan->run();
$jordan->pass();
//Subclass calls the parent class method$jordan->eat("apple");
//Try to call private, directly and through the internal public function//$jordan->age;
$jordan->getAge();echo "<br/>";
$jordan->changePresident("Adam Silver");
echo $jordan->president."<br/>";
echo $james->president."<br/>";
 Let's start with the above example。
What I want here is,Change a variable of two objects at the same time。——usestatic
public static $president="David Stern";
 // Definition of class methodpublic static function changePresident($newP){
static::$president=$newP;// Here, static is more in line with the specifications if it is changed to self}

Pay attention to the static position here, and the method::

The method called has also changed.

echo NbaPlayer::$president;echo "<br/>";
NbaPlayer::changePresident("Adam Silver");
echo NbaPlayer::$president;echo "<br/>";

That is, as mentioned before, a static member is a constant, so it is not for a specific object (not subject to specific objects) - based on this, definition & assignment & call does not require specific objects to participate.

Internal calls should use self/static::$...

External call, class name::

The purpose is the data shared by all objects.

--If the variable is in the parent class when called internally

For example, in the above example, the parent class human writes this sentence

public static $aaa="dafdfa";

Then in the subclass nbaplayer, when calling the static member of the parent class,

echo parent::$aaa;

For external calls, as mentioned above, class name::, so, just the parent class name is just

echo Human::$aaa;

--other

In static methods, other variables cannot be accessed, that is, $this-> cannot be used

--summary

/**
* Static member
* 1. Static attributes are used to save public data of the class
* 2. Only static properties can be accessed in static methods
* 3. Static members can access without instantiating objects
* 4. Inside the class, you can access your own static members through the self or static keywords
* 5. The parent class's static members can be accessed through the parent keyword
* 6. Static members of a class can be accessed externally through the class name
 */

4.4 Final Members

--question

Don't want a certain class to have subclasses;

Don't want the subclass to modify a variable in the parent class (avoid rewriting?)

--final

》=php5 version

Give an example

class BaseClass{
 public function test(){
 echo "BaseClass::test called<br/>";
 }
public function test1(){
 echo "BaseClass::test1 called<br/>";
 }
}
class ChildClass extends BaseClass{
 public function test(){
 echo "ChildClass::test called<br/>";
 }
}
$obj=new ChildClass();
$obj->test();

Writing a method name that is exactly the same as that in the parent class (the content may be different) in the subclass will complete the rewriting of the parent class method!

Therefore, if you do not want to be rewritten, write the final method in the parent class

Copy the codeThe code is as follows:

final public function test(){

And so on, for parent classes that do not want to have subclasses, write final in the class name

Copy the codeThe code is as follows:

final class BaseClass{

--summary

/**
* Rewrite and Final
* 1. Writing methods that are exactly the same as those of the parent class in a subclass can complete the rewriting of the parent class method
* 2. For classes that do not want to be inherited by any class, you can add the final keyword before the class
* 3. For methods that do not want to be overwrite (overwrite, modify) by subclasses, you can add final keyword before the method definition.
 */

4.5 Data access

Remove all final first

--parent

Then write it in the subclass method

parent::test();

After running, you will find that you can still call the parent class through the parent keyword, even the rewritten data.

--self

Then write in the method test in the parent class

self::test1();

After running, I found that self can call data in the same class (other methods/static variables/const)

--summary

/**
* Data access supplement
* 1. The parent keyword can be used to call the class members whose parent class is rewritten.
* 2. The self keyword can be used to access the member methods of the class itself, or to access its own static members and class constants; it cannot be used to access the properties of the class itself; when accessing class constants, you do not need to add a $ symbol before the constant name.
* 3. The static keyword is used to access static members defined by the class itself. When accessing static properties, you need to add a $ symbol before the property name.
 */

4.6 Object Interface

Very important! ! !

--question

Different classes have the same behavior, but the same behavior has different implementation methods.

For example, humans and animals eat, but the way they eat is different.

--definition

An interface defines the common behavior of different classes and then implements different functions in different classes.

--chestnut

Copy the codeThe code is as follows:

//Define an interface
interface ICanEat{
 public function eat($food);
}

It can be seen that there is no specific implementation of methods in the interface, but there must be methods!

Then, here is, "Humans can eat"

//Specific object, connect to the interfaceclass Human implements ICanEat{
public function eat($food){
 echo "Human eating ".$food.".&lt;br/&gt;";
 }
}
$obj=new Human();
$obj-&gt;eat("shit");

Please ignore the "food" I gave.

Note that you no longer use extends, but implements. Then, the same method name is exactly the same. The object then has to/better implement the method.

continue

interface ICanEat{
 public function eat($food);
}
//Specific object, connect to the interfaceclass Human implements ICanEat{
public function eat($food){
 echo "Human eating ".$food.".&lt;br/&gt;";
 }
}
class Animal implements ICanEat{
 public function eat($food){
 echo "Animal eating ".$food.".&lt;br/&gt;";
 }
}
$obj=new Human();
$obj-&gt;eat("shit");
$monkey=new Animal();
$monkey-&gt;eat("banana");

Let the animals eat too!

--Reverse operation

Determines whether an object is connected to an interface.

Copy the codeThe code is as follows:

var_dump($obj instanceof ICanEat);

The boolean value will be returned.

--More Chestnuts

interface ICanPee extends ICanEat{
 public function pee();
}
class Demon implements ICanPee{
 public function pee(){
 echo "Can demon pee?";
 }
 public function eat($food){
 echo "Can demon eat ".$food;
 }
}
$ghost=new Demon();
$ghost->pee();
$ghost->eat("shit");

Interfaces are essentially classes and can be inherited/inherited, but using the interface of the inherited interface, all parent class and "parent class" methods must be implemented in specific ways.

--summary

/**
* Interface
* 1. Basic concepts and basic usage methods of interfaces
* 2. There is no specific implementation of the methods in the interface
* 3. A class that implements an interface must provide methods defined in the interface
* 4. You cannot create an object with an interface, but you can determine whether an object has implemented an interface.
* 5. The interface can inherit the interface (interface extends interface)
* 6. All methods defined in the interface must be public, which is a feature of the interface.
 */

aaaaaaaaaaaaaa

bu xiang xie le....................

ming tian yao ge ..............

The above content is a detailed explanation of PHP object-oriented (three) introduced by the editor to you. I hope you like it.