constructors, access permissions private____ functions

Source: Internet
Author: User

First, the constructor

Classes with constructors Call this method each time a new object is created, so it is ideal to do some initialization before working with the object. If a constructor is defined in a subclass, the constructor of its parent class is not implicitly invoked. To execute the constructor of the parent class, you need to call parent::__construct ()in the constructor of the subclass. If a subclass does not define a constructor, it inherits from the parent class as a normal class method (if it is not defined as private). Second, access privileges private

The parent class private property does not inherit from the quilt class. Same. The parent class private method also does not inherit from the quilt class

Iii. understanding of Polymorphism

When an object invokes a method, the program looks for a method with the same name in the code area in memory, and the program can tell which method to call when there are multiple methods with the same name.

<?php
	class Animal {
		private $age =;
		protected $name;
		protected $sex = ' gong ';
		
		The Public Function __construct ($name) {  //subclass does not automatically call the parent class constructor when the constructor is defined, and the parent class constructor is not automatically invoked when a subclass has a defined constructor.
			$this->name = $name;		
		}
		
		
		Public Function call () {		//polymorphic: When an object invokes a method, the program looks in the code area in memory for a method with the same name, and the program can tell which method is invoked when there are multiple methods with the same name.
		}
	}
	
	Class Dog extends Animal {public
	
	
		function call () {
			echo ' Puppy name: '. $this->name. ' Sex: '. $this->sex;
			Echo '
'; } } Class Cat extends Animal {public function call () { echo ' kitten name: '. $this->name. ' Sex: '. $this->sex; Echo ' Age: '. $this-> $age; Output age error, notice:undefined variable:age } //private property does not inherit from the Quilt class $d = new Dog (' Jack '); $c = new Cat (' Tom '); $array = Array ($d, $c); foreach ($array as $e) $e->call (); ? >
The output results are:

Puppy name: Jack sex: Male
Kitten name: Tom sex: Male
notice:undefined variable:age in f:\xampp\webroots\object\ Extendstest.php on line 29

Fatal error:cannot Access Empty property in f:\xampp\webroots\ Object\extendstest.php on line 29

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.