php : calling method of one class from another

  • super67
  • Born
  • Born
  • No Avatar
  • Joined: Aug 22, 2007
  • Posts: 3
  • Status: Offline

Post August 22nd, 2007, 7:06 am

hi,

i was just wondering how i can access method of one class from another:

for eg:

lets say that i have a method called getNumRows() inside a mysql.class.php
how do i access this method from another method called check() inside functions.class.php

hope i was clear :D

Thanks !
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 22nd, 2007, 7:06 am

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post August 22nd, 2007, 7:25 am

You use the ( -> ) operator to access public member functions and variables of an instantiated class.

PHP Code: [ Select ]
class MySQLClass
 
{
 
    public function getNumRows()
 
    {
 
        return $something;
 
    }
 
}
 
 
 
class MyFunctions
 
{
 
    public function check()
 
    {
 
        $object = new MySQLClass();
 
        $num_rows = $object->getNumRows();
 
    }
 
}
  1. class MySQLClass
  2.  
  3. {
  4.  
  5.     public function getNumRows()
  6.  
  7.     {
  8.  
  9.         return $something;
  10.  
  11.     }
  12.  
  13. }
  14.  
  15.  
  16.  
  17. class MyFunctions
  18.  
  19. {
  20.  
  21.     public function check()
  22.  
  23.     {
  24.  
  25.         $object = new MySQLClass();
  26.  
  27.         $num_rows = $object->getNumRows();
  28.  
  29.     }
  30.  
  31. }
The Beer Monocle. Classy.
  • super67
  • Born
  • Born
  • No Avatar
  • Joined: Aug 22, 2007
  • Posts: 3
  • Status: Offline

Post August 22nd, 2007, 8:53 am

that works fine but what if i want to reuse an existing instance instead of opening a new one.

for eg:
i make use of these two classes in a script called user.php which has already opened an instance.how do i pass this instance to my check() method inside functions.class.php (instead of instantiating a new mysql class ) ?

Thanks :D
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post August 22nd, 2007, 9:30 am

You should just be able to pass the instance variable as an argument to the function and use it directly in the function. If this does not work, you might want to try passing the object by reference.

You might find the following useful as well:
Classes and Objects (PHP5)
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post August 22nd, 2007, 9:41 am

If there's already an instance in existance, you can refer to it within a function using the global keyword.

http://www.php.net/global
Strong with this one, the sudo is.
  • super67
  • Born
  • Born
  • No Avatar
  • Joined: Aug 22, 2007
  • Posts: 3
  • Status: Offline

Post August 23rd, 2007, 6:14 am

thanks for hint guys.the problem is now solved !
  • dev567
  • Born
  • Born
  • No Avatar
  • Joined: Feb 25, 2010
  • Posts: 1
  • Status: Offline

Post February 25th, 2010, 11:22 am

I think this is what you meant:

Code: [ Select ]

class MyClass {

  function Func1() {
    return '1';
  }

  function Func2() {
    echo $this->Func1();
  }

}

$c = new MyClass();
$c->Func2(); // prints: 1
  1. class MyClass {
  2.   function Func1() {
  3.     return '1';
  4.   }
  5.   function Func2() {
  6.     echo $this->Func1();
  7.   }
  8. }
  9. $c = new MyClass();
  10. $c->Func2(); // prints: 1

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 111 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.