Spl_autoload_register parameter Issues

Source: Internet
Author: User
Tags autoload
I see Symfony2 's Psr4classloader class in the following sentence:

public function register($prepend = false)    {        spl_autoload_register(array($this, 'loadClass'), true, $prepend);    }

The first argument to the Spl_autoload_register function is a string, and I can understand that it is adding a function named string to the AutoLoad stack. But what does this mean by an array?

Reply content:

I see Symfony2 's Psr4classloader class in the following sentence:

public function register($prepend = false)    {        spl_autoload_register(array($this, 'loadClass'), true, $prepend);    }

The first argument to the Spl_autoload_register function is a string, and I can understand that it is adding a function named string to the AutoLoad stack. But what does this mean by an array?

This problem indicates that you do not have a few ways to describe and handle callbacks in PHP.
The is_callable can be processed directly by a series of functions, and the parameter is a string or an array that can locate the call location.

There are several callback types in the callable, the main one is the simple callback type, and the passing of a string is the callback function named after the string. There are two types of incoming arrays: one is a static method callback, and the other is an object method callback. The code of the title is the object method callback, call the $this->loadclass () method of this class, of course, if LoadClass is a static method, change $this to the name of the current class as a string, or so write: spl_autoload_register('MyClass::loadClass', true, $prepend); . Here is the callback type official address, you can see: Callback callback Type

If it's an array, it's an object in a class,
Equivalent to $this->loadclass

  • 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.