Summary of PHP-related knowledge point instances

Source: Internet
Author: User
This article mainly introduces PHP-related knowledge points, the final class, final method, abstract class, abstract method, interface, overload, magic method, and other related concepts and operation skills related to the php class are summarized and analyzed based on the instance form, for more information about PHP, see the examples in this article. We will share this with you for your reference. The details are as follows:

Final class and final method

If the method in the parent class is declared as final, the subclass cannot overwrite the method. If a class is declared as final, it cannot be inherited.

final class a{}class a{  final public function A(){}}

Abstract classes and abstract methods

abstract class a {  public abstract function func();}class A extends a{  public function func(){}}

① A class can be declared as an abstract class using abstract statements. an abstract class is a class that cannot be instantiated and can only be used as the parent class of other classes.

abstract class name{}

② A method can use abstract declaration as an abstract method. abstract methods only need to declare the method header without the method body in braces.

abstract public function name($val);

③ If an abstract method exists in a class, the class must be declared as an abstract class.

④ If the subclass inherits from an abstract class, it cannot implement all abstract methods in the parent class, unless the subclass continues to be an abstract class.

⑤ When sub-classes implement methods that abstract parent classes, the access control character range cannot be reduced, and the parameters of methods must be consistent (overwritten ).

Interface

An interface can be viewed as a high-level abstract of an abstract class. it can be said that it is a special case of an abstract class.

interface a{  function func();}class A implements a {  function func(){}}

Class: attribute, class constant, method

Abstract class: attributes, class constants, abstract methods

Interfaces: interface constants and abstract methods

① Compared with abstract classes, interfaces can implement 'multi-inheritance'

class A implements a,b,c{}

② One interface can inherit only one interface

interface a extends b{}

③ Methods are abstract methods without abstract modification. that is, the lower-level classes either implement interface methods (implements) or declare them as abstract methods.

Heavy load Technology

1. attribute overloading: if a non-existent attribute is used, a pre-defined method in the class will be called to process data;

Do the following for scenarios where attributes are not used:

① Value: _ get () in the automatic call class ()
② Value assignment: _ set () in the automatic call class ()
③ Determine whether there exists: _ isset () in the automatic call class ()
④ Destroy: _ unset () in the automatic call class ()

The premise is to pre-define these methods; otherwise, an error will be reported.

 ol[$key])){      return $this->ol[$key];    }    return $key.' not exists';  }  public function __set($key,$val){    $this->ol[$key] = $val;  }  public function __isset($key){    if(isset($this->ol[$key])){      return true;    }    return false;  }  public function __unset($key){    unset($this->ol[$key]);  }}$obj = new bee();echo $obj->one;echo '
';$obj->one = 1;echo $obj->one;echo '
';var_dump(isset($obj->one));

The running result is as follows:

one not exists1boolean true

2. method overload: if a non-existent method is used, a pre-defined method in the class will be called to process the behavior;

Do the following for scenarios where the method is not used:

① Call an inaccessible method in an object: __ call ()
② Call the inaccessible method in static mode: __callstatic ()

 ';    echo 'function '.$name.' not exist';  }  static function __callStatic($name,$arr){    echo '
'; echo 'function '.$name.' not exist'; } /*public function __call($name,$arr){ $num = count($arr); if($num<1){ $name(); }else{ $name = $name.'_one'; $name($arr[0]); } }*/}$obj->func();bee::staFunc();/*$obj->func(1);function func(){ echo '
'; echo 'no.0';}function func_one($val){ echo '
'; echo 'no.'.$val;}*/

The running result is as follows:

function func not existfunction staFunc not exist

Note: All of the above methods are magic methods with parameters

I hope this article will help you with PHP programming.

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.