PHP: Share the 5 most commonly used magic methods

Source: Internet
Author: User
1.__contruct ()

As in English, this is the constructor function. The difference between this and a normal constructor is that you do not have to define a function that has the same name as the class name each time as a constructor.


2.__call ($funcName, $params)

is a function that fires when an instance calls an undefined function in a class

Class model{    //$funcName: undefined    function name//$params (array): Parameters for undefined functions    __call ($funcName, $params) {            //I don't use it here. Params        return "The function: $funcName is not exist!"    }} $model =new model (); $model->func ();  function Func does not exist, it will trigger __call

3.__get ($name)

Used to invoke non-public attributes in a class:

Class model{    Private host = "http://localhost";    Public Function __get ($name) {        return $this, $name;    }} $model =new model (); $model->host;

4.__set ($name, $value)

corresponding to the __get method, used to assign values to non-public properties

Class model{    private host =null;    Public Function __set ($name, $value) {        $this, $name = $value;    }} $model =new model (); $model->host = "http://localhost";

5.__autoload ()

This is used to introduce the class file, generally we use include, include_once, require, require_once to introduce the file

But if we want to introduce multiple files, we should write a good introduction to the statement, so it is not convenient and not beautiful, __autoload can solve the problem

, his greatest advantage is his lazy attributes, when instantiating the object will introduce the corresponding class file.

For example, my Model class file Model.class.php in the current directory of the model/model.class.php (in fact, the introduction of the path of the class file) function __autoload ($className) {    //$className is the class name    $filename = ". /model/". $className. ". Class.php ";    Combination path    require_once "$filename";    Introduction of class file}//This function is triggered whenever an object is instantiated
  • 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.