Share the usage of inheritance during php development

  • 2020-05-07 19:24:07
  • OfStack

inheritance
You usually need classes that have the same variables and functions as other existing classes. In fact, defining a generic class for all projects, and continually enriching the class to fit each specific project, would be a good exercise. To make this point easier, classes can be extended from other classes. An extended or derived class has all the variables and functions of its base class (this is called "inheritance", but no one dies) and contains the parts defined in all derived classes. Elements in a class cannot be reduced, that is, any existing functions or variables cannot be written off. An extended class always depends on a single base class, that is, multiple inheritance is not supported. Use the keyword "extends" to extend 1 class.
 
<?php 
class test { 
public function __construct() { 
} 
public function name() { 
$this->xname('John'); 
} 
private function showName($name) { 
echo 'my name in test is '.$name; 
} 
} 
class extendTest extends test { 
public function __construct() { 
parent::__construct(); 
} 
private function showName($name) { 
echo 'my name in extendTest is '.$name; 
} 
} 
$test = new extendTest(); 
$test->name(); 
?> 

The above example defines a class named Named_Cart, which has all the variables and functions of the Cart class, plus the additional variable $owner and one additional function set_owner(). Now you have a named shopping cart created in the normal way, and you can set and get the owner of the cart. The normal shopping cart function can still be used in the named shopping cart class:
< ?php
$ncart = new Named_Cart; // create a new shopping cart with a name
$ncart- > kris set_owner (" "); // name the shopping cart
print $ncart- > owner; // print the name of the cart owner
$ncart- > add_item (" 10 ", 1); // (functionality inherited from the shopping cart class)
? >
This can also be called a parent-child relationship. Create 1 class, parent class, and use extends to create a new class based on parent class: subclass. You can even use this new subclass to create another class based on it.
Note:
Classes can only be used after they are defined! If you need the Named_Cart class to inherit from the Cart class, you must first define the Cart class. If you need to create another Yellow_named_cart class based on the Named_Cart class, you must first define the Named_Cart class. Simply put: the order in which classes are defined is important.
 
class Person{ 
protected $name;//protected Protected permissions , It is accessible in subclasses, not externally  
protected $age; 
protected $sex; 
function __construct($name,$age,$sex){ 
$this->name=$name;// When using this , even when name No statement, and will be made again 1 a  
$this->age=$age; 
$this->sex=$sex; 
echo "###############"; 
} 
public function say(){ 
echo " My name: {$this->name} My age {$this->age} My gender: {$this->sex}<br/>"; 
} 
protected function eat(){ 
echo "wwwwwwwwwwwwwwwwwwwww<br>"; 
} 
function run(){ 
} 
protected $name;//protected Protected permissions , It is accessible in subclasses, not externally  
protected $age; 
protected $sex; 
} 
// inheritance  
class Student extends Person{ 
var $school; 
function __construct($name,$age,$sex,$school){ 
parent::__construct();// Invokes the constructor of the parent class  
$this->school=$school; 
} 
// overloading say() methods , extend  
protected function say(){// Use the parent class public, A subclass cannot have lower privileges than a parent class , You can drink the same permissions as the parent class  
//Person::say();// Calls the parent class say() methods  
parent::say();// Call the parent class say() Method, parent Represents the parent class name , Can also be called when the superclass name changes.  
echo " My school {$this->school}<br/>";//www.3ppt.com 
} 
function study(){ 
echo "{$this->name} In the study <br/>"; 
} 
} 
$s=new Student("zhangsan",23," male "); 
$s->say(); 
$s->study(); 

* 1. One of the three features of object orientation
*
* 2. Openness and scalability
*
* 3. Increase code reuse
*
* 4. Improved maintainability of software
*
* 5. Inheritance is the use of subclasses to "extend" the parent class
*
* C++ belongs to multiple inheritance, and the same class can have more than one parent class
*
* PHP and JAVA are single inheritance, and the same class can only have one parent class
*
* you can have multiple subclasses, whether multiple or single
*
* as long as you have members you can share when designing two classes, use the content you can share as a single base class
*
* 1. Application of class inheritance
*
* 1. Declare a subclass, using the extends keyword to inherit (extend) a parent class
*
* 2. Subclasses can inherit everything from their parent classes, including member property methods, constructors... , can be used in subclasses
*
* 2. Access type control
*
* while subclasses can inherit everything from their parent, private private members can only be used in this class and not in subclasses
*
* when encapsulating, you can make both the inside of your class accessible and the inside of your subclass available, but not the outside of your class, as long as you set the permissions to protected
*
*
*
* 3. Method of overloading parent class in subclass
*
* 1. A subclass can declare the same method name as a parent class, that is, a subclass overrides a method with the same name as a parent class
*
* 2. Subclass methods extend superclass methods
*
* 3. Call the overridden method in the superclass in the subclass
* use parent class name :: method name () parent:: method name ()
*
* 4. Write a constructor in a subclass. If there is a constructor in a parent class, 1 must call the overridden constructor in the parent class once
*
* note: methods overloaded in subclasses cannot be lower than the access permissions in the parent class.

Related articles: