Encapsulation, inheritance, class static methods, static properties of PHP functions, classes and objects, and classes

Source: Internet
Author: User

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

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.