Calling Class Functions from within OTHER Class Functions...

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
UKSouthernLad
Forum Newbie
Posts: 5
Joined: Sun Jan 04, 2009 6:12 am

Calling Class Functions from within OTHER Class Functions...

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post 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()
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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;
    }
}
UKSouthernLad
Forum Newbie
Posts: 5
Joined: Sun Jan 04, 2009 6:12 am

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

Post 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. :-)
UKSouthernLad
Forum Newbie
Posts: 5
Joined: Sun Jan 04, 2009 6:12 am

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

Post by UKSouthernLad »

Can *ANYONE* tell me, if it's possible to call a METHOD of CLASS "B" whilst in a METHOD OF CLASS "A"???
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

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

Post by Syntac »

Yes. And there's no need to shout. :evil:
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post 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";
 }
}
 
 
UKSouthernLad
Forum Newbie
Posts: 5
Joined: Sun Jan 04, 2009 6:12 am

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

Post 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)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post by jaoudestudios »

This is why we ask people to post code! And not pages of it, the part they think are suspect! :teach:
Post Reply