views:

244

answers:

3

Hi,

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?

+5  A: 

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)

dbemerlin
Thanks! Yes I know I know very little about full OOP (PHP5), but unfortunately my boss won't give me a day off to read up on something he wants fixed now. :) Tbh, I don't really know why you have static and public before a method?? But I'll get there. Thanks again for the help.
Constant M
Basically `public` means you can access the function as such `$object->func()` whereas if it was `private` you couldn't... and `static` means you can't pass variables to it and you have to use `::` to access the function. So not `$object->func()` but `Class::func()`. Anyone correct me please if I'm wrong. Also, please accept his answer if it was helpful (by clicking the green check icon)! You have a really low accept rate (14%) so I would suggest you go back and accept any answers you did use to avoid getting flamed here.
Axsuul
Thanks for all the tips!
Constant M
+1  A: 

'self' is the keyword you're looking for

that said, can you explain why you need your methods to be static? "static" is poor style and should be avoided.

stereofrog
I wouldn't say that 'static' is poor style eventhough i agree in this case that it might not have been used correctly. static has a place and sometimes not making something static would be bad style. I agree though that many people use static methods wrongly and use classes only like you would use C++ namespaces with only static methods => _that_ is bad style.
dbemerlin
definitely, static has its uses, but they are mostly fairly advanced. Beginners should be told to avoid static altogether.
stereofrog
+3  A: 
Tatu Ulmanen
and what if he wants the method to be static?
Natrium
@Natrium, then he can use what I suggested first.
Tatu Ulmanen
sorry, must 've had sand in my eyes...
Natrium