Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:42:35+00:00 2026-05-27T15:42:35+00:00

I am writing a php app with subclasses and since I want to have

  • 0

I am writing a php app with subclasses and since I want to have multiple ways to create an object I am doing different factory methods instead of multiple constructors.

I have a User with factory methods

User::from_id
User::from_netid

I have several subclasses of User. I was previously calling the parent superconstructor, but when I switched to the factory method that constructor didn’t exist.

I have Student, a subclass of User. To get it to work, I had to duplicate almost all of my superclass factory code in User::from_id to load_by_id, since in this situation the instance already existed:

// In Student.php - Student extends User
public static function from_id_and_course($id, $course){
    $instance = new self();
    $instance->load_by_id($id);
    $instance->course = $course;
    ...
}

I want to call the superclass factory method from the subclass as a starting point, and then continue to add the other fields. Something like this…

$instance = User::from_id($id);

or

$instance = Student::from_id($id);

but in these cases it gives me a User object, and I need a Student object. The only way I could accomplish this is by doing $instance = new self().

How can I call the superclass factory method from the subclass as a starting point to create a new subclass factory method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:42:36+00:00Added an answer on May 27, 2026 at 3:42 pm

    Your problem is this:

    $instance = new self();
    

    self refers to the class where the method is defined, not the caller:

    • When Student::from_id() is called, if it doesn’t exist, it falls back to User::from_id().
    • In User::from_id(), self refers to User, not Student.

    You’d have to use late-static bindings:

    $instance = new static();
    

    However, like I always do, I’d highly recommend against it. You’re better off using the object scope than the static scope. It’s easier to extend, to fake or mock and incidentally, to test.

    There’s nothing wrong with:

    $user = new User;
    $user->from_id($id);
    
    $student = new Student;
    $student->from_id($id);
    

    …it’s actually better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a PHP app and have several classes that only have static methods (no
I'm writing a simple web app in PHP that needs to have write access
I'm writing server-side programs in PHP for an iPhone app. And I have no
I am writing a pretty basic php app and have created a pretty big
I am writing a php/mysql app that uses a few different tables. Table 1
I have followed a tutorial to create a simple blog writing application in PHP
Hi I'm writing and app in PHP. I want to use jquery form validation
i'm writing a chat app with php/mysql i have 3 tables: user, room and
I'm writing a php app to access a MySQL database, and on a tutorial,
I'm writing my first PHP app that has to directly deal with dates, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.