Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling a method from another method in same PHP class

I'm trying to use a method from within another method in a class. I don't have much experience in PHP5 OOP, and I looked around for answers, but couldn't find any. I'm trying to use getClientInfo() in sendRequest(), which is in the same class.

class DomainHandler {      public static function getClientInfo($db, $client_id)     {         //Do stuff     }      public static function sendRequest($details)     {          require_once('MySQL.class.php');         $db = new MySQL;          getClientInfo($db, $client);     } } 

And it tells me:

Fatal error: Call to undefined function getClientInfo()

I've also tried

parent::getClientInfo($db, $client);  

and

$this->getClientInfo($db, $client); 

to no avail.

Any ideas?

like image 241
Constant Meiring Avatar asked Feb 08 '10 10:02

Constant Meiring


People also ask

How can we call one method from another method in php class?

You need to call like $this->a(); php.net/manual/en/language.oop5.php documentation is your friend. You need to use $this->a(), $this represent current class. so current class call function a. RTFM It's PHP OOP basics!

Can a method call another method in the same class?

Similarly another method which is Method2() is being defined with 'public' access specifier and 'void' as return type and inside that Method2() the Method1() is called. Hence, this program shows that a method can be called within another method as both of them belong to the same class.

How do you call a method inside a function in php?

php class class_name { function b() { echo 'test'; } function c() { // This function belongs inside method "c". It accepts a single parameter which is meant to be an instance of "class_name". function a($that) { $that->b(); } // Call the "a" function and pass an instance of "$this" by reference.

Can a method call another method?

We can't directly pass the whole method as an argument to another method. Instead, we can call the method from the argument of another method.


1 Answers

It's a static method so you have to call it with self::getClientInfo or DomainHandler::getClientInfo.

Also: You might want to read up on object oriented programming since it looks like you have not yet understood what it's really about (it's not just putting functions between a class Foo { and } and putting public static in front of them)

like image 186
Morfildur Avatar answered Sep 30 '22 15:09

Morfildur