PHP functions, classes and objects, and class encapsulation, inheritance, class static methods, static properties, PHP static
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 ''; Var_dump ($var); }
2. Classes (Class) and objects (new OBJ)
// defines a person's class and is not yet an 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::eat4. Static methods and static properties of classes
PHP// define a URL-related class url{ publicstaticfunction CreateURL ($arr = []) { echo__method__; } publicstaticfunction Redirect ($url = ") { echo__method__; } Public Static function Getcurrenturl () { echo__method__; }} // declare methods as static methods, do not need to instantiate each time (new) object, easy to operate, save memory, more efficient Echo Url::createurl ($var= "); Echo Url::redirect ($url= "); Echo Url::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'
'; EchoSelf::$var; Echo'
'; 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='');
http://www.bkjia.com/PHPjc/1034505.html www.bkjia.com true http://www.bkjia.com/PHPjc/1034505.html techarticle PHP functions, classes and objects and classes of encapsulation, inheritance, class static methods, static properties, PHP static 1, function PHP built-in functions can be used directly, if not installed PHP extension can ...