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 9241421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:22:10+00:00 2026-06-18T08:22:10+00:00

Whilst trying to debug some PHP classes, I ran into some behaviour which is,

  • 0

Whilst trying to debug some PHP classes, I ran into some behaviour which is, to my mind, really weird.

I’ve constructed a demonstration of the behaviour below:

class BaseClass {
   public function baseMethod () {
      echo (implode (' ', $this -> childMethod ()) . PHP_EOL);
   }
}

class ChildClass extends BaseClass {
   protected function childMethod () {
      return array ('What', 'The', 'Actual', 'Fork!');
   }
}

$a = new ChildClass ();
$a -> baseMethod ();

Now, to my mind, the base class should not be able to make any assumptions about the subclass at all, except for the ones it enforces for the subclass by declaring (or inheriting) abstract methods, or by implementing an interface. However, the above code actually outputs a string and doesn’t throw any errors!

What The Actual Fork!

This seems like broken behaviour to me. Unless the base class declares abstract protected function childMethod();, it should not be able to call it, should it?

I’ve been scouring the internet to try and find something that demonstrates that this is expected behaviour. So far all I’ve managed to find is the following from PHP’s manual:

Visibility from other objects

Objects of the same type will have access to each others private and
protected members even though they are not the same instances. This is
because the implementation specific details are already known when
inside those objects

So is the behaviour I’m witnessing here correct or is this a bug in PHP? It’s certainly not behaviour I’d rely on because it seems wrong to me.

FYI, the problem we found in the actual code was that the subclass declared a private method that the superclass was trying to call. The superclass didn’t declare the method abstract (and if it had done it would have had to be at least protected).

  • 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-18T08:22:11+00:00Added an answer on June 18, 2026 at 8:22 am

    Now, to my mind, the base class should not be able to make any
    assumptions about the subclass at all, except for the ones it enforces
    for the subclass by declaring (or inheriting) abstract methods, or by
    implementing an interface.

    That’s how things work in statically typed languages. In PHP (and many others) you are free to call any method on any value (it need not even be an object). If the call does not make sense, you get a runtime error.

    Of course it’s a good idea to declare the method if you expect derived classes to implement it; this is simply good practice. Additionally, many well-known PHP IDEs will detect such an omission and flag the call to bring it to your attention exactly because the compiler cannot.

    This seems like broken behaviour to me. Unless the base class declares
    abstract protected function childMethod();, it should not be able to
    call it, should it?

    It should, as explained above. If you find this behavior undesirable, PHP is not the language you should be using.

    Aside: In a closely related note, you might also find it counter-intuitive that a base class can access protected members defined in a derived class without any trouble. This is documented. In fact it is exactly what happens in your example, but I mention it specifically because it’s a different kind of counter-intuitive (your example would also be as puzzling if the method were public).

    Consider the endless list of constructs that PHP allows which also “should not be possible” from a statically typed perspective, which include:

    // #1
    $varName = 'foo';
    
    // How do you know $object has a property named "foo"?
    // How do you know that "foo" is a valid property name in the first place?
    // How do you know that $object is an object to begin with?
    echo $object->$varName;
    
    // #2
    $object = new SomeObject;
    $methodName = 'someMethod';
    
    // it's practically impossible to reason about this before runtime
    call_user_func(array($object, $methodName));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst 'investigating' finalisation (read: trying stupid things) I stumbled across some unexpected behaviour (to
Gday All, I have a baffling problem whilst trying to insert some chinese characters
I'm trying to join tables events and venues , however I ran into an
I've come across a weird problem whilst trying to optimise the following image: It
I’ve run into a little bit of an annoyance whilst trying to rebind a
I've run into a slight issue whilst trying to embed the Justin.tv player into
I am developing a Spring/Hibernate application and I am seeing some unexpected behaviour whilst
I'm new to VBA and Access in general and ran into this problem whilst
I'm new to PHP and trying to create the following whilst minimizing the amount
Whilst using Xcode 4.0 and trying to debug and see what value is held

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.