Page 1 of 1

Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 6:14 am
by UKSouthernLad
Hi

A - Can someone tell me if you can call a Class Function from within ANOTHER (Different Class) Function.
B - If this is dependant on a specific version of PHP, what version is this?

Thnx

----------------
Now playing: Mariah Carry - I miss you
via FoxyTunes

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 6:21 am
by jaoudestudios
Do you mean call a method not a class function?

Yes you can. I dont think it is specific to a version of php, it has always been around. It just depends how you do it.
i.e. make an instance of the object...$instance = new class()...$instance->method(), or if it is static call it directly...$class::method()

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 7:09 am
by Eran
Inside object methods (or functions), you have access to the $this scope which is a reference of the object. You can use it to invoke other methods or access members of the object:

Code: Select all

<?php
class Dog {
    public function bark() {
        $this -> say('bark'); //$this is the object instance
    }
 
    public function say($text) {
        echo $text;
    }
}

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 7:43 am
by UKSouthernLad
pytrin wrote:Inside object methods (or functions), you have access to the $this scope which is a reference of the object. You can use it to invoke other methods or access members of the object:

Code: Select all

<?php
class Dog {
    public function bark() {
        $this -> say('bark'); //$this is the object instance
    }
 
    public function say($text) {
        echo $text;
    }
}


Thank you, but thats not quite what I need.... I need to call class functions (methods yes thanks) from methods in DIFFERENT classes. :-)

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 1:51 pm
by UKSouthernLad
Can *ANYONE* tell me, if it's possible to call a METHOD of CLASS "B" whilst in a METHOD OF CLASS "A"???

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 2:07 pm
by Syntac
Yes. And there's no need to shout. :evil:

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 2:08 pm
by jaoudestudios
Yes, PLEASE dont shout. READ http://us.php.net/manual/en/language.oop5.basic.php!!!
We are giving our time to help you!
Just create the object.

Code: Select all

 
class A {
 public function a() {
   echo "aaa";
   $b = new b(); // create instance of object
   $b->b();  // call method of object b
 }
}
 
class B {
 public function b() {
   echo "bbb";
 }
}
 
 

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 2:50 pm
by UKSouthernLad
My apologies, was not my intention to shoult, merely to emphasise my points...

My problem in this case was I did not put () after $objUser->fnSearchEmail;


(WHAT A SPANNER!!! LOL)

Re: Calling Class Functions from within OTHER Class Functions...

Posted: Sun Jan 04, 2009 3:00 pm
by jaoudestudios
This is why we ask people to post code! And not pages of it, the part they think are suspect! :teach: