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?
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!
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With