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: eat4. 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 = '');