CarlosWong
CarlosWong

Reputation: 13

Codeigniter: instantiate_class function()

When I read the source of Codeigniter, I found a function located in file:system/codeigniter/comm.php like that:

/**
* Instantiate Class
*
* Returns a new class object by reference, used by load_class() and the DB class.
* Required to retain PHP 4 compatibility and also not make PHP 5.3 cry.
*
* Use: $obj =& instantiate_class(new Foo());
*/
function &instantiate_class(&$class_object)
{
    return $class_object;
}

I can't undestand it well, and I need more details about the compatibility things. Thanks.

update: Codeigniter version is 1.7.2

Upvotes: 1

Views: 1234

Answers (1)

hakre
hakre

Reputation: 198237

The function and it's example use in question is totally bogus:

$obj =& instantiate_class(new Foo());

That's the same as writing:

$obj =& new Foo();

Which is the way that needed to be done in PHP 4.

Since PHP 5, you can just write:

$obj = new Foo();

as you might know.

Actually, as you, I do not see any use of the function just for the sole purpose that it's superflous. It might have had some need in the past (when it had some other definition). I can only assume about it's existance, most likely that the developer had problems to understand references/aliasing in PHP which is commonly not well understood.

Edit: As KingCrunch pointed out, there is a need for this function if you would like to glue PHP 4 with PHP 5.3 code, because it will prevent strict standards warnings in PHP 5.3. So instead of the PHP 4 (potentially needed)

$obj =& new Foo();

This must be written as

$obj =& instantiate_class(new Foo());

Which hides away the "Assigning the return value of new by reference is deprecated" warning and allows to code in a PHP 4 style for a PHP 5.3 environment.

Hope this helps you with your software archaeology.

Upvotes: 5

Related Questions