Part one of learning PHP object-oriented programming
Inherit
1. Class members defined in the parent class do not need to be repeatedly defined in the subclass, saving programming time and cost
2. Subclasses of the same parent class have the same class members defined by the parent class, so external code can treat them equally when calling them.
3. Subclasses can modify and adjust class members defined by the parent class
<?php
class Animal {
private $weight;
public function getWeight()
{
return $this->weight;
}
public function setWeight($w)
{
$this->weight = $w;
}
}
class Dog extends Animal
{
/**
*子类新增方法
*/
public function Bark()
{
echo "Wang~~Wang~~~ ";
}
}
$myDog = new Dog();
$myDog->setWeight(20);
echo "Mydog's weight is ".$myDog->getWeight().'<br>';
$myDog->Bark();
?>
Copy after login
Access Control
1. Three types of object-oriented permissions
(1) public: shared class members, which can be accessed in the classroom
(2) protected: protected class members can be accessed by itself and its subclasses
(3) Private: Private class members can only be accessed by themselves.
Static keyword (static)
1. Static attributes are used to save the common data of the class
2. Only static properties can be accessed in static methods
3. Static members can be accessed without instantiating the object
4. Inside the class, you can access its own static members through the self or static keyword
5. Static members of the parent class can be accessed through the parent keyword
6. Static members can be accessed outside the class definition through the name of the class
final member
1. For classes that do not want to be inherited by any class, you can add the final keyword
before class
2. For methods that do not want to be overridden by subclasses, you can add the final keyword
in front of the method definition.
Data Access
1. The parent keyword can be used to access methods in the parent class that are overridden by the subclass
2. The self keyword can access the member methods of the class itself, and can also be used to access its own static members and class constants; it cannot be used to access the properties of the class itself; when using constants, there is no need to add the '$' symbol in front of 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 the '$' symbol in front of the property.
http://www.bkjia.com/PHPjc/953322.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/953322.htmlTechArticlePHP object-oriented programming learning part one Inheritance 1. Class members defined in the parent class do not need to be repeated in the subclass Definition, saving programming time and cost 2. Subclasses of the same parent class have similar...