Page 1 of 1
Singleton function in class that's extended.
Posted: Fri Jul 13, 2007 9:53 am
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?
Posted: Fri Jul 13, 2007 10:07 am
by stereofrog
We had this discussion recently see
viewtopic.php?p=399628#399628 and below
Re: Singleton function in class that's extended.
Posted: Fri Jul 13, 2007 4:52 pm
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.
Posted: Fri Jul 13, 2007 11:14 pm
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.
Posted: Mon Jul 16, 2007 7:25 pm
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?
Posted: Mon Jul 16, 2007 10:33 pm
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.
Posted: Mon Jul 16, 2007 10:52 pm
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.
Cheers
Posted: Fri Jul 20, 2007 10:37 am
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.