0

I am wondering if such this subclassing structure is possible in PHP. If this is a duplicate I apologize as I couldn't figure out what this process would be called and the only I could find was a closed question with no answer.

I am working with multiple classes. For example we will say Main and User.

The Main class will hold all of the initiation code. So

class Main{
  //Setters for core variables and data needed to make calls
  ...
  protected function fetchInfo(){
      //Do Stuff to return info from other source(Curl calls in my case)
  }

}

and User

class User extends Main{
    public function getName(){
        $data = $this->fetchInfo();
        return $data['name'];
    }
}

But instead of having it where I would. Do $exampe1 = new Main(...); to set varaibles, and $example2 = new User(); to call the subclass to do $example2->getName(); is there a way to do something like $example = new Main(); whcih could then call the subclasses when needed like $example->User->getName();?

I know there are a lot of different ways this could be handled, but I want the classes separate for organization and I plan on having a lot of subclasses that need to pull info from that main class and would like to know if there is a way they can be linked in that fashion.

EDIT: The reason I dont want to just call User calls to get the function is I'll end up having 15+ classes that handle the returned data differently and making wonder if there was a better way than making a new Object for each one if I want to use it.

3
  • user knows all about main, but main knows nothing about user -- so to speak, so no you have to use User to call getName but i dont see why that would be an issue Commented Oct 29, 2017 at 23:21
  • Yea there is no breaking issue. Its only that i will eventually have 15+ classes and was wondering if there was a way to do so I not declaring all those classes to pull from different parts of the classes and have a simple tack on this piece to show you mean this class. Commented Oct 29, 2017 at 23:35
  • calling User or whatever is the right approach over calling Main this is not an unusual set up by the way Commented Oct 29, 2017 at 23:39

1 Answer 1

1

A "Main" is not a "User" so I would say this type of subclassing is a poor choice.

I might instead look at injection.

class MainDataHandler {
  //...
}

class User {

  private $main;

  public function __construct(MainDataHandler $main) {
    $this->main = $main;
  }

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

The benefits of injection is that your classes can work and be tested independently without dependencies on another class to do the work. Also if "Main" ever changes you your User class isn't dependent on how the new Main works.

Sign up to request clarification or add additional context in comments.

2 Comments

Yea The Main class is just a core data handling and the subclasses will be organized classes of handing the data received. So Users, Accounts, Games, Teams. They all pull from the same Data location. The User class will just say if you want to pull user information tack on this. If you want to pull Game Data tack the Game class instead.
Or make main (gasp!) a singleton, and reference it that way without injection....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.