43

I'm still learning OOP so this might not even be possible (although I would be surprised if so), I need some help calling another classes method.

For example in ClassA I have this method:

function getName()
{
    return $this->name;
}

now from ClassB (different file, but in the same directory), I want to call ClassA's getName(), how do I do that? I tried to just do an include() but that does not work.

Thanks!

4
  • share the code you are using to call getName() Commented Nov 21, 2012 at 23:31
  • Does class B have any relation to class A (such as hierarchical)? If so, Class B can extend class A and you will have access to class A's functions. If not, can you describe your two objects a little more (what they represent). Commented Nov 21, 2012 at 23:33
  • The thing is, Class A is already extending another class (simple_html_dom that I downloaded from the net) so I dont want to (dont even know if I can) extend Class A which is already extending some other class Commented Nov 21, 2012 at 23:34
  • @evert, I dont have any code... I was hoping someone could give me some code to do what I wrote above as I am stuck. Commented Nov 21, 2012 at 23:35

4 Answers 4

95
//file1.php
<?php

class ClassA
{
   private $name = 'John';

   function getName()
   {
     return $this->name;
   }   
}
?>

//file2.php
<?php
   include ("file1.php");

   class ClassB
   {

     function __construct()
     {
     }

     function callA()
     {
       $classA = new ClassA();
       $name = $classA->getName();
       echo $name;    //Prints John
     }
   }

   $classb = new ClassB();
   $classb->callA();
?>
Sign up to request clarification or add additional context in comments.

8 Comments

This is the one you want, even though it is currently negative.
Note that you cannot easily unit test your B class now, because you have tightly coupled to A class to it.
right - that include should probably be in a controlling file that calls both A and B instead of B including A. In this example you would always be instantiating A when you load B. But it should be easy for you to separate.
That's another lesson for the OP to learn another day... all the OP needs now is basics. Thanks for pointing that out anyway
Better learn the right way than starting on the wrong foot imo.
|
22

If they are separate classes you can do something like the following:

class A
{
    private $name;

    public function __construct()
    {
        $this->name = 'Some Name';
    }

    public function getName()
    {
        return $this->name;
    }
}

class B
{
    private $a;

    public function __construct(A $a)
    {
        $this->a = $a;
    }

    function getNameOfA()
    {
        return $this->a->getName();
    }
}

$a = new A();
$b = new B($a);

$b->getNameOfA();

What I have done in this example is first create a new instance of the A class. And after that I have created a new instance of the B class to which I pass the instance of A into the constructor. Now B can access all the public members of the A class using $this->a.

Also note that I don't instantiate the A class inside the B class because that would mean I tighly couple the two classes. This makes it hard to:

  1. unit test your B class
  2. swap out the A class for another class

3 Comments

This one is good too but you specify two different files. So you just have to remember to include the class you want to instantiate with "new B()", etc
I read the question more as how can I access a member of another class than how can I include a file, but yeah you are right. Although autoloading files is something everybody should do for convenience imho.
Yeah definitely. Though at this stage I think autoloading might be overloading the learning process... perhaps.
11

You would need to have an instance of ClassA within ClassB or have ClassB inherit ClassA

class ClassA {
    public function getName() {
      echo $this->name;
    }
}

class ClassB extends ClassA {
    public function getName() {
      parent::getName();
    }
}

Without inheritance or an instance method, you'd need ClassA to have a static method

class ClassA {
  public static function getName() {
    echo "Rawkode";
  }
}

--- other file ---

echo ClassA::getName();

If you're just looking to call the method from an instance of the class:

class ClassA {
  public function getName() {
    echo "Rawkode";
  }
}

--- other file ---

$a = new ClassA();
echo $a->getName();

Regardless of the solution you choose, require 'ClassA.php is needed.

1 Comment

ClassA::getName(); is the charm.
0

File 1

class ClassA {

    public $name = 'A';

    public function getName(){
        return $this->name;
    }

}

File 2

include("file1.php");
class ClassB {

    public $name = 'B';

    public function getName(){
        return $this->name;
    }

    public function callA(){
        $a = new ClassA();
        return $a->getName();
    }

    public static function callAStatic(){
        $a = new ClassA();
        return $a->getName();
    }

}

$b = new ClassB();
echo $b->callA();
echo $b->getName();
echo ClassB::callAStatic();

1 Comment

See my comment under @codingbiz's answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.