On the collation of classes

Source: Internet
Author: User

<?PHP//the three main characteristics of a class: encapsulation, inheritance, polymorphism//purpose of encapsulation: In order to make classes more secure//How to encapsulate: when declaring a member property or member method, the encapsulation of the member is implemented using the Private keyword decoration, and the encapsulated member can only be accessed in this class and cannot be accessed externallyclassperson{Private$name; Private$sex; Private$age; function __construct ($sex="male")    {        $ This->sex=$sex; } function Run () {echo"is walking"; }     Privatefunction Say () {return "Sing a song"; } function __set ($name, $value) {$ This$name =$value; } function __get ($name) {return$ This-$name; }} $p=NewPerson ();//$p->name= "Simon";//You cannot assign a value to a private member externally//$p->say (); //private methods cannot be called externally//Var_dump ($p);$pRun (); $p->name="Simon"; Var_dump ($p); Echo $p-name;//access to Private members: two, one is function SetName () {} and Function GetName () {} write directly, so that fewer members can be used, more each to write increase the amount of code;//the other is the __set () and __get () Magic method automatically called;//Inheritance: Inheritance is a subclass of the existing parent class to expand the function, a parent class can have more than one child class, a subclass only a parent class, many with inheritance is one-way, subclass can inherit all of the parent class (except private)//Inherit keyword extends//There are three types of access modifiers: public (accessible to anyone), protected (which can be accessed by themselves and subclasses), private (except for those who cannot access them)//the method of overloading a parent class in a subclass: In fact, overriding a method that inherits from a parent class can define a method with the same name as the parent class, which can be parented: A method name, a new member that is newly declared in a subclass, and assign a value//The final keyword, which can be added to a class or method, cannot identify a member property, uses a final identified class, represents the last, cannot be inherited, and cannot be overridden in a subclass by using the member method of the final identity//The static keyword visit identifies either a member variable or a member method, regardless of the object, and static identifies the class//Static access mode: Class Name:: static member variable name; class Name:: Static member Method name ();//Self , which represents the class-like object-$this,self: static member variable name; Self:: Static member Method name ();//Const defines a member property in a class as a constant, without the addition of a $ character before using a constant declared by Cinst .//Clone Cloning Object//Load Class//include methodInclude"Info.class.php");//load the class file into the page, note that the parameter is a path (find the class file)Include"/wamp/www/0429/info.class.php";//another way to loadRequire_once"./info.class.php";//Request a target file onceRequire_once ("./info.class.php"); require ("Info.class.php");//include method if there is a fatal error in the class, the file with this class will be error can not continue execution, require method if the file with this class error, the following file will continue to execute//auto Load class:function __autoload ($classname)//All classes are uniform naming rules, all classes must be written in the same folder, the class name and the file name, you can use this method{include ($classname.". class.php");}//current directory./or file name//Parent Directory: ///Subordinate directory: Directory name///root directory:///root directory if it is in PHP code/represents the root of the local disk (D-Drive)//if it is inside the HTML/represents the current site directory (WWW)// polymorphic//Concept: When a parent class refers to a subclass instance, there is a subclass that overrides the parent class function, which causes us to behave differently when using the reference to invoke the corresponding method.//occurrence Condition: 1. Must have inheritance//subclasses must override methods of the parent class//function overloading (strongly typed language only)//function Overloading Condition: 1. Function names must be the same;//2. The number of parameters inside the function is different//3. If the number is the same, the type is different//The method name must be the same as the method name, the number of arguments is different, the number of parameters is the same, the type of the parameter is not the same//PHP is a weakly typed language, so no overloads//PHP functions with variable parameters//__tostring () method?>

On the collation of 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.