The concept of object-oriented programming:
Different authors may disagree, but an OOP language must have the following:
Abstract data types and information encapsulation
Inherited
Polymorphic
In PHP, the encapsulation is done through a class:
<?php class Something {//In the OOP class, the first character is usually an uppercase Var $x; function SetX ($v) {//method starts as a lowercase word, and then uses uppercase letters to separate words, such as Getvalueofarea () $this->x= $v; } function GetX () {return $this->x; } }
Of course you can define your own preferences, but it's better to keep a standard, which is more effective. Data members are defined in a class using the "var" declaration, which is not typed until the data member is assigned a value. A data member can be an integer, an array, an associated array (associative array), or an object. method is defined as a function in a class, you should use $this->name when accessing a class member variable in a method, otherwise it can only be a local variable for a method.
Use the new operator to create an object: $obj =new Something;
Then you can use the member function to pass: $obj->setx (5); $see = $obj->getx ();
In this example, the SETX member function assigns 5 to the object's member variable x (not the Class), and then Getx returns its value 5. It is not a good OOP habit to access data members by way of a class reference like $obj->x=6. I strongly recommend accessing member variables through methods. If you consider the member variable to be non-processing and use the method only through the object handle, you will be a good OOP programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is also allowed in PHP. Inheritance is easy to implement in PHP, as long as the Extend keyword is used.
<?php class Another extends Something {var $y; function Sety ($v) {$this->y= $v; } function GetY () {return $this->y; } }
objects of the "another" class now have all the data members and methods of the parent class (Something), plus their own data members and methods.
PHP does not now support multiple inheritance, so you cannot derive new classes from two or more of the two classes. You can redefine a method in a derived class, and if we redefine the Getx method in the "Another" class, we cannot use the Getx method in "Something". If you declare a data member in a derived class that has the same name as implementation, it will "hide" the data members of the base class when you process it.
You can define constructors in your class. A constructor is a method that has the same name as a class, and is called when you create an object of a class, for example:
<?php class Something {var $x; function Something ($y) {$this->x= $y; } function SetX ($v) {$this->x= $v; } function GetX () {return $this->x; }} So you can create an object by: $obj =new Something (6); The constructor automatically assigns a value of 6 to the data variable, x. Constructors and methods are normal PHP functions, so you can use the default parameters. function Something ($x = "3", $y = "5") Then: $obj =new Something (); X=3 and y=5 $obj =new Something (8); X=8 and y=5 $obj =new Something (8,9); X=8 and Y=9
The default parameter uses C + +, so you can't ignore the value of Y, and give X a default parameter, which is assigned from left to right, and if the passed parameter is less than the required parameter, the default parameter is used.
When an object of a derived class is created, only its constructor is called, the constructor of the parent class is not called, and if you want to call the constructor of the base class, you must display the call in the constructor of the derived class. You can do this because the methods of all the parent classes in the derived class are available.
Object-oriented Programming for PHP