call local function in the class [singltone]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Crazy Coder
Forum Newbie
Posts: 7
Joined: Thu Dec 14, 2006 8:55 am

call local function in the class [singltone]

Post 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 ??
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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();
?>
Crazy Coder
Forum Newbie
Posts: 7
Joined: Thu Dec 14, 2006 8:55 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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() ;
Crazy Coder
Forum Newbie
Posts: 7
Joined: Thu Dec 14, 2006 8:55 am

Post 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() ;
:D Thanks bro

that what I'm looking for

god bless you
Post Reply