PHP Learning Note 4-class/Namespace/member Method/class method

Source: Internet
Author: User

Namespaces namespace Classes Class
Create file Hello.php,namespace is Jikexueyuan:

1<?PHP2 /**3 * Created by Phpstorm.4 * User:administrator5 * DATE:2015/6/286 * time:13:417  */8 //name Space9 namespace Jikexueyuan;Ten  One  A classHello { -      Public functionSayHello () { -         Echo' Hello jikexueyuan<br/> '; the     } -}

Re-create the file Hello.php,namespace is jkxy:

1<?PHP2 /**3 * Created by Phpstorm.4 * User:administrator5 * DATE:2015/6/286 * time:13:447  */8 //name Space9 namespace Jkxy;Ten  One  A classHello { -      Public functionSayHello () { -         Echo' Hello jkxy<br/> '; the     } -}

Create file index.php:

1<?PHP2 /**3 * Created by Phpstorm.4 * User:administrator5 * DATE:2015/6/286 * time:12:437  */8 Header("content-type:text/html; Charset=utf-8 ");9 require_once' jkxy/hello.php ';//require_once: Calling a fileTen require_once' Jikexueyuan/hello.php '; One  A $h=New\jkxy\hello ();//to create an instance of a class - $h->sayhello ();//accessing internal functionality through an instance -  the $d=New\jikexueyuan\hello (); - $d->sayhello ();

Invoking files jkxy/hello.php and jikexueyuan/hello.php with require_once , accessing the internal functionality of the class by instantiating the class

Construction method: Executes when an instance of the class is created

Create File man.php

1<?PHP2 /**3 * Created by Phpstorm.4 * User:administrator5 * DATE:2015/6/286 * time:13:567  */8 9 classMan {Ten  One     /** A * @param int $age age//type of parameters passed in - * @param string $name name -      */ the      Public function__construct ($age,$name){ - //Echo ' Construct a man '; -         $this->_age =$age;//log down through this -         $this->_name =$name; +         } -     } +      Public functionGetage () {//The outside world can access A         return $this-_age; at     } -  -      Public functionGetName () { -         return $this-_name; -     } -  in     Private $_age,$_name; -}

Used in index.php in the following ways:

1 require_once ' man.php '; 2 $m New Man ("Vito"); // is executed when an instance of the class is created 3 Echo $m-GetName (); 4 Echo $m->getage ();

Examples of member methods:

1  Public function getage () {// member Method 2         return$this-_age; 3     }

static methods/Static constants/Static variables add the following code to the man.php:

1  Public Static function Say_hello () {// static method: Mainly used to describe class properties 2         echo "Hello PHP"; 3     }4     privatestatic$NUM = 0;   static variable 5     const max_man_num = n;   Static Constants

To execute a static method in index.php:

1 Man::say_hello (); // executing a static method

Add code to man.php and use static and static constants in index.php:

1 man::$NUM+ +; // Add each time you create 2         if (Man::$NUM>man::max_man_num) {3             Throw New Exception ("Cannot create more"); 4         }

Using static variables and static constants in index.php

1  for ($i= 0; $i<Man::MAX_MAN_NUM; $i+ +) {2     new man ("Vito"); 3 }

PHP Learning Note 4-class/Namespace/member Method/class method

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.