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

  • Home
  • SEARCH
  • 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 8852267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:21:23+00:00 2026-06-14T13:21:23+00:00

If I have an abstract class like this: abstract class MyApp { public function

  • 0

If I have an abstract class like this:

abstract class MyApp
{

   public function init()
   {
      $this->stuff = $this->getStuff();

   }
   public function getStuff()
   {
      return new BlueStuff();
   }
}

And then I have a class that extends from this abstract class like this:

class MyExtendedClass extends MyApp
{
   public function init()
   {
      parent::init();
   }

   public function getStuff()
   {
      return new RedStuff();
   }
}

If I do:

$myobj = new MyExtendedClass();
$myobj->init();

Why does the method getStuff from the child class get called? Isn’t $this in the context of the abstract class? If so, shouldn’t the method of the abstract class get called?

Thanks!

  • 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-06-14T13:21:25+00:00Added an answer on June 14, 2026 at 1:21 pm

    New answer

    In PHP you can use subclasses as if all methods in the parent class that don’t exist in the subclass have been copied to the subclass. So your example would be the same as:

    class MyExtendedClass extends MyApp {
        public function init() {
            $this->stuff = $this->getStuff();
        }
    
        public function getStuff() {
            return new RedStuff();
        }
    }
    

    Just think of the subclass as having all code of the parent class and you’re normally all right. There is one exception to this rule: properties. A private property of a class can only be accessed by that class, subclasses can’t access the private properties of parent classes. In order to do that you need to change the private property into a protected property.


    Original answer

    Abstract classes in PHP are just like regular classes with one big difference: they can’t get initiated. This means you can’t do new AbstractClass();.

    Since they work exactly like regular classes for everything else, this also is the case for extending classes. This means that PHP first tries to find the method in the initiated class, and only looks in the abstract classes if it doesn’t exist.

    So in your example this would mean that the getStuff() method from MyExtendedClass is called. Furthermore, this means you can leave out the init() method in MyExtendedClass.

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

Sidebar

Related Questions

I have a class structure like abstract class Animal { public Animal(){ //init stuff..
I have a model that looks something like this: public abstract class Parent {
If I have an abstract class like this: public abstract class Item { private
I have an abstract entity base class defined like this: public abstract class SessionItem
I have an abstract class BaseItem declared like this: public abstract class BaseItem {
I have a class hierarchy like this public abstract class CalendarEventBase{} public class TrainingEvent
If I have a base class like this that I can't change: public abstract
I have defined an Action pure abstract class like this: class Action { public:
I have an abstract class like this public abstract class Abstractclass { public Abstractclass(int
I have a class like this public abstract class BaseType<T> { public string Name

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.