php : calling method of one class from another

  • super67
  • Born
  • Born
  • super67
  • Posts: 3

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

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. }
  • super67
  • Born
  • Born
  • super67
  • Posts: 3

Post 3+ Months Ago

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
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

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)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

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
  • super67
  • Born
  • Born
  • super67
  • Posts: 3

Post 3+ Months Ago

thanks for hint guys.the problem is now solved !
  • dev567
  • Born
  • Born
  • dev567
  • Posts: 1

Post 3+ Months Ago

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 158 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
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.