dslreports logo
 
    All Forums Hot Topics Gallery
spc
Search similar:


uniqs
1242

PToN
Premium Member
join:2001-10-04
Houston, TX

PToN

Premium Member

PHP: spl_register_autoload - Symfony question

Hello,

I have a question with the UniversalClassLoader.php that ships with Symfony 2.

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

As per PHP docs, the first argument in the call is the function to call, the custom loader, instead of the __autoload that ships with PHP.

In the code above what does "array($this, 'loadClass')" refer to...? I know what $this is and what loadClass is, but i am not clear on what they mean there...

Anyone know why they include the object itself in there..??

Thanks.

cdru
Go Colts
MVM
join:2003-05-14
Fort Wayne, IN

cdru

MVM

PHP is not my native language, but I believe the array($this, 'loadClass') refers to a local method loadClass that is within UniversalClassLoader class.

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD
ARRIS SB6141
Ubiquiti EdgeRouter Lite
Ubiquiti UniFi AP

1 edit

JAAulde to PToN

MVM

to PToN
cdru See Profile is correct. When you call the `register` method on an instance of UniversalClassLoader, that instance's `loadClass` method is passed to `spl_autoload_register()`.

PToN
Premium Member
join:2001-10-04
Houston, TX

PToN

Premium Member

Ok.

I know that $this references to itself, but i didnt find any mention of spl_register_autoload() taking an array as the callback param. I only saw its use in implementations, but never understood WTF the array() was doing there when the docs said: " callable $autoload_function "

Thanks.

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD
ARRIS SB6141
Ubiquiti EdgeRouter Lite
Ubiquiti UniFi AP

JAAulde

MVM

A lot of the SPL stuff is documented in a minimal fashion (to put it nicely). Almost any PHP function which can take a string function-name to specify a callback can be given an array to specify a static class method or an object's instance method instead of just a function.

cdru
Go Colts
MVM
join:2003-05-14
Fort Wayne, IN

cdru to PToN

MVM

to PToN
The second and last user contributed notes on teh spl_autoload_register manual page both address passing in an array.