Home > Backend Development > PHP Tutorial > Summary of several aspects of object-oriented inheritance in php

Summary of several aspects of object-oriented inheritance in php

黄舟
Release: 2023-03-12 11:22:01
Original
1016 people have browsed it

This article mainly summarizes several pointsphp object-orientedA few points when inheriting:

//people.class.php
	class People{
		private $name;
		private $sex;
		private $birthday;
		
		private function construct($name='',$sex='01',$birthday='1999-01-01'){
			echo &#39;people---construct<br>&#39;;
			$this->name = $name;
			$this->sex = $sex;
			$this->birthday = $birthday;
		}
		public function get($key){
			return $this->$key;
		}
		public function set($value,$key){
			$this->$key = $value;
		}
		public function show(){
			return &#39;people---&#39;;
		}
	}
Copy after login
//student.class.php
	class Student extends People{
		private $s_num;
		private $s_class;
		public function construct($name,$sex,$birthday,$num,$class){
			//parent::construct($name,$sex,$birthday);
			echo &#39;Student--construct<br>&#39;;
			$this->name = $name;
			$this->sex = $sex;
			$this->birthday = $birthday;
			$this->s_num = $num;
			$this->s_class = $class;
		}
		public function showInfo(){
			return &#39;sutdent---&#39;.$this->name.&#39;----bir=&#39;.$this->birthday
			.&#39;num==&#39;.$this->s_num.&#39;----class==&#39;.$this->s_class;
		}
	}
Copy after login

The above two classes Student inherit the People class

The constructor method of the parent class is private, which means in java This class cannot be inherited, but in php, this class can be inherited, but one thing is that the constructor of the parent class cannot be called in the subclass Student

parent::construct($name,$sex,$birthday);
Copy after login

Otherwise, an error will be reported, and if the constructor of the parent class is private, then the subclass must have its own constructor and must be written clearly, otherwise Inheritance is not possible.

At the same time, what is different from java is that when a subclass inherits a parent class and the subclass has its own constructor, the parent The constructor of a class will not be executed unless called from the constructor of a subclass.



##

The above is the detailed content of Summary of several aspects of object-oriented inheritance in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template