PHP functions, classes and objects, and class encapsulation, inheritance, class static methods, static properties, PHP static _php tutorial

Source: Internet
Author: User

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::eat

4. 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 ...

  • Related Article

    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.