10 Replies - 1735 Views - Last Post: 28 July 2009 - 08:24 AM Rate Topic: -----

#1 [RTS]BN+VS*  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 174
  • Joined: 23-March 09

Calling abstract functions

Posted 22 July 2009 - 05:31 PM

Hey,

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
Is This A Good Question/Topic? 0
  • +

Replies To: Calling abstract functions

#2 [RTS]BN+VS*  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 174
  • Joined: 23-March 09

Re: Calling abstract functions

Posted 22 July 2009 - 06:22 PM

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
Was This Post Helpful? 0
  • +
  • -

#3 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Calling abstract functions

Posted 22 July 2009 - 11:48 PM

You can't have abstract static functions! :)

Quote

BN+VS*' date='23 Jul, 2009 - 03:22 AM' post='710586']
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

Was This Post Helpful? 0
  • +
  • -

#4 [RTS]BN+VS*  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 174
  • Joined: 23-March 09

Re: Calling abstract functions

Posted 23 July 2009 - 06:06 AM

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
Was This Post Helpful? 0
  • +
  • -

#5 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Calling abstract functions

Posted 23 July 2009 - 06:14 AM

I think the final keyword would be useless for a static method since you can't override a static method/function. And no you can't call abstract methods from a static method since the static method does not belong to an instance which all implemented abstract methods would do. What you can do is to provide a class with a static method that require's an object instance that derives from a certain abstract class, and call that objects implemented version of the abstract method you need.

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

BN+VS*' date='23 Jul, 2009 - 03:06 PM' post='711085']
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

Was This Post Helpful? 0
  • +
  • -

#6 [RTS]BN+VS*  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 174
  • Joined: 23-March 09

Re: Calling abstract functions

Posted 23 July 2009 - 09:11 AM

Ok, thnx! This helped me out :)

Cheers
Jeroen De Dauw
Was This Post Helpful? 0
  • +
  • -

#7 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Calling abstract functions

Posted 23 July 2009 - 09:24 AM

Great! :)

Quote

BN+VS*' date='23 Jul, 2009 - 06:11 PM' post='711224']
Ok, thnx! This helped me out :)

Cheers
Jeroen De Dauw

Was This Post Helpful? 0
  • +
  • -

#8 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Calling abstract functions

Posted 25 July 2009 - 06:08 AM

It seems to be fine having abstract static functions in PHP! Sorry about that, I have been coding in C# for too long.

View PostWimpy, on 23 Jul, 2009 - 08:48 AM, said:

You can't have abstract static functions! :)

Was This Post Helpful? 0
  • +
  • -

#10 AdaHacker  Icon User is online

  • Resident Curmudgeon

Reputation: 438
  • View blog
  • Posts: 792
  • Joined: 17-June 08

Re: Calling abstract functions

Posted 25 July 2009 - 11:18 AM

View PostWimpy, on 25 Jul, 2009 - 07:08 AM, said:

It seems to be fine having abstract static functions in PHP! Sorry about that, I have been coding in C# for too long.

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.
Was This Post Helpful? 1
  • +
  • -

#11 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Calling abstract functions

Posted 25 July 2009 - 11:30 AM

Thanks for sorting it out! I really thought I was correct the first time but when I was reading through some forums today I found someone actually using abstract static functions so I tried it on my Windows box (PHP 5.2.8, I think) and it was working just fine! :)

This post has been edited by Wimpy: 25 July 2009 - 11:31 AM

Was This Post Helpful? 1
  • +
  • -

#12 [RTS]BN+VS*  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 174
  • Joined: 23-March 09

Re: Calling abstract functions

Posted 28 July 2009 - 08:24 AM

Thanks for the info guys :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1