Reputation: 2502
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?
Upvotes: 3
Views: 6471
Reputation: 197624
Replace HandsetDetection()
/ prevent it from being called by giving the extending class a constructor method (Demo):
<?php
class A {
function A() {
echo "A() constructor\n";
}
}
class B extends A {
function __construct() {
echo 'B() constructor', "\n";
}
function A() {
echo "override\n";
}
}
$x = new B();
Then things should turn out well. For an explanation about the background info, see @edorians answer with links to the manual etc.
Upvotes: 2
Reputation: 38961
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.
<?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"
.
Upvotes: 11