Original: PHP functions, classes and objects, and class encapsulation, inheritance, class static methods, static properties
1. Functions
PHP built-in functions can be used directly, if not installed PHP extension can be
Custom functions
// functional Function function Dump ($varnull) { // expense default parameter value echo ' < Pre/>'; Var_dump ($var); }
2. Classes (Class) and objects (new OBJ)
<? // Define a person's class, not now object class//
Private $eye= ' Big Eyes '; Private $mouth= ' small Mouth '; Private $leg= ' Big long legs '; //custom Call when constructing method new Object Public function__construct () {Echo __class__; } Public functionrun () {Echo $this-leg; } //Learning uses legs (walking), eyes (Reading), mouth (studying) Public functionStudy () {Echo $this->leg,$this->eye,$this-mouth; } }//after using class new, it becomes an object.$person=NewPerson ();//Output Person$personRun ();//large long leg output$personStudy ();//output big long legs big eyes small lips
3. Encapsulation of classes (public, protected, private) and inheritance (extends)
//Inheritance of Classesclassa{ Public functionHelp () {Echo __method__; } //declare a way to eat private Private functioneat () {Echo __method__; }}//subclasses can inherit all public methods and properties of the parent class, protected methods and properties, except private methods//If you want to use them, override themclassBextendsa{ Public functiondosomething () {$thisHelp ();//The method that inherits the parent class is declared as public Echo' ; $thisEat ();//because eating is private and subclasses are not available, a Eat method is written in the subclass. } //sub-class eating Public functioneat () {Echo __method__; }}$b=NewB;$b->dosomething ();
Output results
A::help
B::eat
4. Static methods and static properties of classes
<?PHP//define a URL-related classclassurl{ Public Static functionCreateURL ($arr= []) { Echo __method__; } Public Static functionredirect$url= ' ') { Echo __method__; } Public Static functionGetcurrenturl () {Echo __method__; }}//declare methods as static methods, do not need to instantiate each time (new) object, easy to operate, save memory, more efficientEchoUrl::createurl ($var= ' ');EchoUrl::redirect ($url= ' ');EchoUrl::getcurrenturl ();
5. Static properties of the class
<?PHP//define a URL-related classclassurl{//declared as a class constant ConstURL = ' http://www.baidu.com ';//You can use the const//declaration as a static variable directly outside of the class since 5.3 Public Static $var= ' It is very good '; Public Static functionCreateURL ($arr= []) { EchoSelf::URL; Echo' <br/> '; EchoSelf::$var; Echo' <br/> '; Echo __method__; } Public Static functionredirect$url= ' ') { Echo __method__; } Public Static functionGetcurrenturl () {Echo __method__; }}//declare methods as static methods, do not need to instantiate each time (new) object, easy to operate, save memory, more efficientEchoUrl::createurl ($var= ");
Encapsulation, inheritance, class static methods, static properties of PHP functions, classes and objects, and classes