Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to override PHP class / function

I am unable to override a function in a child class at my local Ubuntu test LAMP server, but the very same code is resulting in the desired override when uploaded to a webserver.

Original class:

class HandsetDetection {
    function HandsetDetection() {
        //code I wish to replace
    }
}

My class:

class HandsetDetection_RespondHD extends HandsetDetection {
    function HandsetDetection() {
        //code I wish to use
    }
}

Constructors aren't involved.

The version of PHP in use on my local machine is PHP 5.3.3-1ubuntu9.5 with Suhosin-Patch (cli) (built: May 3 2011 00:48:48)

The version of PHP on the webserver where the override is successful is 5.2.17

Can you think why this may be?

like image 563
David Oliver Avatar asked Feb 22 '26 09:02

David Oliver


1 Answers

I would assume it has something to do with the fact that the class has the same name as the method.

It's a php4 style constructor.

As long as the class is not in a namespace that is still an issue.

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.


Example:

<?php


class A {

    function A() {
        echo "construct";
    }
}

class B extends A {

    function A() {
        echo "override";
    }

}

$x = new B();

This will output "construct"

now calling

$x->A();

will output "override".

like image 191
edorian Avatar answered Feb 23 '26 23:02

edorian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!