Singleton function in class that's extended.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Singleton function in class that's extended.

Post by []InTeR[] »

I have the following idee/code:

Code: Select all

<?php
	highlight_file(__FILE__);

	class base
	{
		public static $name = 'base';
		
		function &staticInstance()
		{
			static $instance;
			if(!isset($instance)){
				$instance = & new self::$name;
			}
			return $instance;
		}
	}
	
	class test extends base
	{
		var  $num = 0;
		public static $name = 'test';
		function num($num)
		{
			$this->num += $num;
			echo $num;
		}
	}

	

	$a = test::staticInstance();
	var_dump($a);

	
	$a->num(3);

?>
Now, you would expect a output like '3'.
But! The function makes a base class, instead of a test class.
So you get a fetal error, that the num function doesnt exists.

Any idee's about how you can make a singleton function work in this setup?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

We had this discussion recently see viewtopic.php?p=399628#399628 and below
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Re: Singleton function in class that's extended.

Post by kyberfabrikken »

[]InTeR[] wrote:Any idee's about how you can make a singleton function work in this setup?
Button's Law of Design Maturity

Seriously - You don't need static members. Just instantiate the class, when needed. If you have to reuse the instance in multiple places in your code, there are much better ways, than using a Singleton.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Singletons are heavily used in PHP for some reason it seems. I found a good explanation as to why however.

Singletons make testing quite painful, which is certainly why I rarely, if ever, use one.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

feyd wrote:Singletons are heavily used in PHP for some reason it seems. I found a good explanation as to why however.

Singletons make testing quite painful, which is certainly why I rarely, if ever, use one.
Happen to know here there may be more dialog about this on the internet?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Oops, I meant to say "I have not found a good explanation as to why however."

There have been several threads here where we've talked about singletons and their overuse. Some may be in the moderhood instead of "public;" I can't recall those specifics.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

feyd wrote:Oops, I meant to say "I have not found a good explanation as to why however."

There have been several threads here where we've talked about singletons and their overuse. Some may be in the moderhood instead of "public;" I can't recall those specifics.
No prob. I found a thread at Sitepoint that was pretty good. :D

Cheers
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Post by ev0l »

You can't.

PHP's implementation of static functions and members is broken. Static methods do not inherent there context. They always belong to the class in which they are defined. This makes them nothing more than functions with a funky name. There are a number of design patters that are simply not easily reproducible using PHP because of this.

So far the PHP developers view this as the intended behavior and have been unwilling to rectify.
Post Reply