-1

I have two classes, class A and class B, i am trying to access class B through a function in class A.

At the moment i am able to do this by creating a new instance of class B inside of the function in class A, but i want to know if there is a way to do this without creating a new instance of class B inside of class A.

Here is an example of what i would want to do:

class a{

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


}

class b extends a{

    function b(){
        return "It Works!";
    }

}

$doIt = new a();
echo $doIt->a();
K.D
  • 147
  • 1
  • 12
  • It doesn't work that way. You can not instantiate A and access methods B. You can instantiate B and access methods in A. – Halfstop Feb 10 '19 at 22:21
  • so instantiating B from A is the only option? – K.D Feb 10 '19 at 22:23
  • 2
    Why are you trying to do this? What is your goal? Perhaps we could suggest a better way to accomplish it? The way you suggested seems to break the oop model. –  Feb 10 '19 at 22:25
  • Well, A doesn't know about B, but B knows about A. You can use A, but it doesn't know anything about B. – Halfstop Feb 10 '19 at 22:25
  • Basically i have class B that works with the database, and class A that connects the users actions on the site to class B. So user does something->class A starts working->Passes info on to class B to add to the database. It did seem that this is not all that possible, but i know there are people with a lot more knowledge than I have so i wanted to see if anyone else knew of some way to do this. – K.D Feb 10 '19 at 22:30

2 Answers2

2

You can't. But you can expect an abstract method.

abstract class a {
    function a() {
        return $this->b(); // OK
    }
    abstract function b(); // Has no method body. Classes extending this class will implement it
}
class b extends a {
    function b() {
        return 'something';
    }
}
$doIt = new b(); // Cannot instantiate abstract class a
echo $doIt->a();
emarref
  • 1,286
  • 9
  • 18
  • Is that the equivalent to a c++ virtual function? –  Feb 10 '19 at 22:26
  • No, it's rather equivalent to a C++ pure virtual function. Unless decorated with `final`, all functions are by default equivalent to C++ virtual functions. – Ulrich Eckhardt Feb 10 '19 at 22:28
  • So like int method() = 0; Got you. Very cool. Good to know that all php methods act as virtual functions. –  Feb 10 '19 at 22:29
2

From your comment:

Basically i have class B that works with the database, and class A that connects the users actions on the site to class B. So user does something->class A starts working->Passes info on to class B to add to the database. It did seem that this is not all that possible, but i know there are people with a lot more knowledge than I have so i wanted to see if anyone else knew of some way to do this.

As you describe: B works with the database, A performs actions (requested by the user). B sounds like some sort of mapper/data access object, while A sounds like a service (I'll refer to BMapper and AService to be more deliberate).

What you've outlined indicates you are not using PHP inheritance correctly. Inheritance represents "is a" relationships. (Image is a Media, PathMover is an Animator, etc...). While @Malcolm's answer technically works, it is not for your scenario.

In OOP we would say that BMapper is a dependency of AService. Thus whenever we are calling a method in AService, we would expect it to depend on an instance of BMapper. This relationship is satisfied typically through dependency injection (see: What is dependency injection?).

<?php

class AService {

    private $bMapper;

    public function __construct(BMapper $bMapper)
    {
        $this->bMapper = $bMapper;
    }

    public function doAction()
    {
        return $this->bMapper->performSomethingInDatabase();
    }

}

class BMapper {

    public function performSomethingInDatabase()
    {
        /**
         * ..whatever
         */
    }

}

$service = new AService(new BMapper);
$service->doAction();

You can see that we've defined our dependent class's dependency in its constructor, to be satisfied by whoever/whatever is using that class.

jeremy
  • 9,965
  • 4
  • 39
  • 59