Php functions, classes and objects, class encapsulation, inheritance, class static methods, static attributes, php static

Source: Internet
Author: User

Php functions, classes and objects, class encapsulation, inheritance, class static methods, static attributes, php static

   

1. Functions

Php built-in functions can be used directly. If php extensions are not installed

Custom Functions

// Function name: function dump ($ var = null) {// default expenditure parameter value: echo '<pre/>'; var_dump ($ var );}

2. class and object (new Obj)

<? Php // defines a Person's class. The class Person is not a private attribute.
Private $ eye = 'Big eyes '; private $ mouth = 'small mouth'; private $ leg = 'long legs '; // constructor calls public function _ construct () {echo _ CLASS __;} public function run () {echo $ this-> leg ;} // you can use leg (walking), eye (reading), and mouth (reading) public function study () {echo $ this-> leg, $ this-> eye, $ this-> mouth ;}/// the object $ person = new Person () will become after class new is used; // The output Person $ person-> run (); // output long legs $ person-> study (); // output big legs, big eyes, and small lips

3. Class encapsulation (public, protected, private) and inheritance (extends)

// Class inheritance class A {public function help () {echo _ METHOD __;}// declare A private function eat () {echo _ METHOD __;}}// subclass can inherit all public methods and attributes of the parent class, protected methods and attributes, except for private methods // if you want to use them, override class B extends A {public function doSomething () {$ this-> help (); // The premise for inheriting the parent class method is to declare it as public echo '    
// Output result

A: help

B: eat

4. Static methods and static attributes of Classes

<? Php // define a class Url related to a Url {public static function createUrl ($ arr = []) {echo _ METHOD __;} public static function redirect ($ url = '') {echo _ METHOD __;} public static function getCurrentUrl () {echo _ METHOD __;}} // declare the method as a static method. You do not need to instantiate (new) objects every time. This method is easy to operate, saves memory, and is more efficient. echo Url: createUrl ($ var = ''); echo Url: redirect ($ url = ''); echo Url: getCurrentUrl ();

5. Static attributes of a class

<? Php // define a class Url related to a Url {// declare it as a class constant const URL = 'HTTP: // www.baidu.com '; // After 5.3, you can directly use const outside the class // declare it as a static variable public static $ var = 'it is very good '; public static function createUrl ($ arr = []) {echo self: URL; echo '<br/>'; echo self: $ var; echo '<br/>'; echo _ METHOD __;} public static function redirect ($ url = '') {echo _ METHOD __;} public static function getCurrentUrl () {echo _ METHOD __;}/// declare the METHOD as a static METHOD. You do not need to instantiate (new) objects every time. The operation is convenient, memory saving, and the efficiency is higher. echo Url :: createUrl ($ var = '');

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.