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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:02:31+00:00 2026-05-22T21:02:31+00:00

In a nutshell: a class inherits a function from its parent. This function then

  • 0

In a nutshell: a class inherits a function from its parent. This function then gets called on the child, but appears to still have the scope of the parent class. Is this expected behavior?

Consider the following code example:

<?php
class OLTest {
    private $var1 = 10;

    public function getVar1() {
        if (isset($this->var1)) {
            return $this->var1;
        } else {
            return 'undefined';
        }
    }

    public function getVar2() {
        if (isset($this->var2)) {
            return $this->var2;
        } else {
            return 'undefined';
        }
    }
}

class OLTest2 extends OLTest {
    private $var1 = 11;
    private $var2 = 20;
}

$oltest = new OLTest();
$oltest2 = new OLTest2();

echo "calling parent->getVar1\n";
echo $oltest->getVar1() . "\n";

echo "calling parent->getVar2\n";
echo $oltest->getVar2() . "\n";

echo "calling child->getVar1\n";
echo $oltest2->getVar1() . "\n";

echo "calling child->getVar2\n";
echo $oltest2->getVar2() . "\n";
?>

To my understanding, the output should be:

calling parent->getVar1
10
calling parent->getVar2
undefined
calling child->getVar1
11
calling child->getVar2
20

The actual output on my machine is:

calling parent->getVar1
10
calling parent->getVar2
undefined
calling child->getVar1
10
calling child->getVar2
undefined

To add to the confusion, a print_r($this) within either of the functions, will show that the scope is really set to the subclass, yet it’s impossible to access the variable.

Can someone clear this up for me?

EDIT: I am using PHP in version 5.3.3-1ubuntu9.5.

  • 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-22T21:02:32+00:00Added an answer on May 22, 2026 at 9:02 pm

    No, the output should definetly be 10, undefined, 10, undefined.
    The reason is that the variables that are private are visible only to the class defining them (not super or subclasses).

    So, when child is called, its methods are defined in the parent object and when they resolve var1 or var2 they check in the scope definied for OLTest.
    var2 is not accessible too because the declared var2 is only visible inside OLTest2.

    To get your output, your should declare these variables protected or public.

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

Sidebar

Related Questions

In C# 4 in a Nutshell, the author shows that this class can write
Possible Duplicate: Why we need Thread.MemoryBarrier()? From O'Reilly's C# in a Nutshell: class Foo
I have this code copied from C# 4.0 in a nutshell that uses Attribute/Reflection
Here's what I've been trying to do, in a nutshell: class example <T extends
I was reading Algorithms in a Nutshell (O'Reilly) and came across this symbol in
My controller, in a nutshell is this: chart1.SeriesCollection.Add(SC); using (MemoryStream ms = chart1.GetChartStream()) {
Here s what I'm doing in a nutshell. In my class's cpp file I
I opened a ticket for this problem. In a nutshell here is my model:
I have a div class that looks something like this: <div class=objects> X </div>
I'm attempting to make a messaging system in which any class derived from Messageable

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.