Page 1 of 1
call local function in the class [singltone]
Posted: Wed Dec 27, 2006 10:32 am
by Crazy Coder
see this example :
Code: Select all
Class Ab {
Function Ab ($x)
{
$this->AC($x)
}
Function AC ($x)
{
print $x;
}
}
$MainClass = New MainClass;
$MainClass->print = $MainClass::Ab;
if I need to access AC Function I should use ( this ) operator. but if I run the class as singlton the (this) operator will point to the MainClass . there is no local (this) for the Ab Class.
how can I call the AC function in this stat ??
Posted: Wed Dec 27, 2006 11:02 am
by RobertGonzalez
You're going to have to clarify that a little bit. You are not even remotely coding a singleton class. What is it you are trying to do?
Posted: Wed Dec 27, 2006 11:04 am
by Burrito
if you're trying to call the method statically, you don't need to call it from the object.
Code: Select all
<?
class MainClass
{
function AC()
{
return "somethign";
}
}
echo MainClass::AC();
?>
Posted: Wed Dec 27, 2006 11:26 am
by Crazy Coder
Everah wrote:You're going to have to clarify that a little bit. You are not even remotely coding a singleton class. What is it you are trying to do?
Sorry for my bad english.
If I made the singlton in wrong way can you please help me to know the correct way ?
I didn't find good article talking about singlton classes in PHP4, All of it was for PHP5.
thanks for your help
Burrito wrote:if you're trying to call the method statically, you don't need to call it from the object.
Code: Select all
<?
class MainClass
{
function AC()
{
return "somethign";
}
}
echo MainClass::AC();
?>
thanks for you help.
say that I must call it from the object.
I want to make the Session class in my script as singltone.
the AC Function find out the group and call the group function ( AC or AX or AY ) to do what we need.
Posted: Wed Dec 27, 2006 11:44 am
by RobertGonzalez
Maugrim the Reaper put together a
Patterns for PHP web site. There is
a page on Singletons and there is information on PHP4 Singletons on that page.
Posted: Wed Dec 27, 2006 1:18 pm
by Begby
To answer what I think is your first question, what you are doing is calling a static method and from that static method want to call another method of your object. You use the 'self' keyword instead of 'this' like:
Code: Select all
class Something
{
var $burrito ;
function AA()
{
return self::AB() ;
}
function AB()
{
return self::$burrito ;
}
}
$taco = Something::AA() ;
Posted: Wed Dec 27, 2006 1:56 pm
by Crazy Coder
Begby wrote:To answer what I think is your first question, what you are doing is calling a static method and from that static method want to call another method of your object. You use the 'self' keyword instead of 'this' like:
Code: Select all
class Something
{
var $burrito ;
function AA()
{
return self::AB() ;
}
function AB()
{
return self::$burrito ;
}
}
$taco = Something::AA() ;

Thanks bro
that what I'm looking for
god bless you