Calling abstract functions
Page 1 of 110 Replies - 1735 Views - Last Post: 28 July 2009 - 08:24 AM
#1
Calling abstract functions
Posted 22 July 2009 - 05:31 PM
I have an abstract class with one final function, and 3 abstract ones. I'd like to call the abstract ones (so the code that will be put in the child classes) in the final function.
Is this possible? If yes: how?
Cheers
Jeroen De Dauw
Replies To: Calling abstract functions
#2
Re: Calling abstract functions
Posted 22 July 2009 - 06:22 PM
#3
Re: Calling abstract functions
Posted 22 July 2009 - 11:48 PM
Quote
This page demonstrates how to do this, but not for static functions (all my functions are static) : http://us2.php.net/m...p5.abstract.php
#4
Re: Calling abstract functions
Posted 23 July 2009 - 06:06 AM
The problem I'm having is that the final function in my abstract class needs to be static (can't change that sadly). So apparently It's not possible to call abstract methods from there.
I suspect there are however some workarounds for this - for example the factory model? Any suggestions on which is best suited?
Cheers
Jeroen De Dauw
#5
Re: Calling abstract functions
Posted 23 July 2009 - 06:14 AM
An example:
<?php
class MyClass
{
public static function SayHello(MyOtherClass $obj)
{
$obj->TheFunction();
}
}
abstract class MyOtherClass
{
function TheFunction();
}
class MyDerivingClass extends MyOtherClass
{
public function TheFunction()
{
echo "Hello World";
}
}
MyClass::SayHello(new MyDerivingClass());
?>
Hope it helps!
---- Edit
After thinking about it for 2 seconds you can actually do it like this as well:
<?php
abstract class MyClass
{
public static function SayHello(MyClass $obj)
{
$obj->TheFunction();
}
function TheFunction();
}
class MyOtherClass extends MyClass
{
public function TheFunction()
{
echo "Hello World";
}
}
MyClass::SayHello(new MyOtherClass());
?>
Quote
Ok - they (the abstract ones) don't have to be static.
The problem I'm having is that the final function in my abstract class needs to be static (can't change that sadly). So apparently It's not possible to call abstract methods from there.
I suspect there are however some workarounds for this - for example the factory model? Any suggestions on which is best suited?
Cheers
Jeroen De Dauw
This post has been edited by Wimpy: 23 July 2009 - 06:16 AM
#6
Re: Calling abstract functions
Posted 23 July 2009 - 09:11 AM
Cheers
Jeroen De Dauw
#7
Re: Calling abstract functions
Posted 23 July 2009 - 09:24 AM
Quote
Ok, thnx! This helped me out
Cheers
Jeroen De Dauw
#8
Re: Calling abstract functions
Posted 25 July 2009 - 06:08 AM
#10
Re: Calling abstract functions
Posted 25 July 2009 - 11:18 AM
Wimpy, on 25 Jul, 2009 - 07:08 AM, said:
Actually, no, you were right the first time. I think. Sort of.
According to this page, as of PHP 5.2, abstract static methods are no longer allowed. However, this seems to be inconsistent between versions. On my Ubuntu box running PHP 5.2.6, trying to declare an abstract static method threw a fatal error. However, on my Windows box running PHP 5.2.9, it didn't show any error at all and, in fact, worked just fine. The main documentation on OOP doesn't really say anything one way or the other, so it seems like it's not really safe to dependon these working.
Either way, it might not help the OP anyway, because there's still the late static binding problem, which wasn't fixed until PHP 5.3. Since the static abstract functions would be resolved to the abstract parent class and not the child class, you couldn't call them from the final function in the abstract class anyway, because they'd have no implementation.
#11
Re: Calling abstract functions
Posted 25 July 2009 - 11:30 AM
This post has been edited by Wimpy: 25 July 2009 - 11:31 AM
#12
Re: Calling abstract functions
Posted 28 July 2009 - 08:24 AM
|
|

New Topic/Question
Reply




MultiQuote



|