A summary of some knowledge points for the inheritance of PHP classes

Source: Internet
Author: User

<?php
//1. When encapsulating a class, it is generally safe to use the protected property protected for encapsulation
//2. Methods for calling parent classes by subclasses
//(1). The parent class method property should take public or protected properties protected
//(2). Private properties can only be accessed in this class
//3. Subclasses can override methods that do not require a parent class method to overwrite
//4. When a subclass overrides a parent class method or field, what happens when it is called?
//Call Method Parent class Name:: Method () or Parent:: Method ()
The //5.final keyword prevents classes from being inherited, and separate classes must use this keyword when they do not want to be integrated with other classes, and it is best to add this keyword

//Keywords
//1.public Public property class itself and subclass are accessible
///2.private The Private property class itself can be accessed
///3.protected The protected attribute class itself and subclasses are accessible (typically used for this property for security purposes)
//4.extends Inheritance (PHP does not support multilayer inheritance, a class can inherit only one base class)




header (' content-type:text/html;charset= ' Utf-8 "');
//Define a parent class
class Computer
{

//public public properties, which can be called by the parent class child class
//private private properties, which cannot be inherited by the quilt class, the protected adornments are applied to encapsulate
//protected protected Property The parent subclass subclass can be called
Public $name = ' Gaofei ';

//Define a method
Public function say ()
{
return "My name is". $this->name;
}
}

//define a subclass
class Notecomputer extends computer
{
The //subclass does not require the fields and methods of the parent class, overrides the parent class with overridden methods, and so on fields
Public $name = ' James ';

Public function say ()
{
echo Computer::say ();
echo "My name is". $this->name;
//Call the parent class method that has been overridden
}
}

//Instantiation of Class
$obj = new Notecomputer ();
//echo $obj->say ();

//Plus keyword final The subclass cannot inherit the parent class.
Final class Pad
{
Public $data = ' 123 ';
}

class Pad_pro extends Pad
{
Public function say ()
{
return $this->data;
}
}



A summary of some knowledge points for the inheritance of PHP classes

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.