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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:02:33+00:00 2026-05-29T15:02:33+00:00

I have a class first where $name is set to ‘bob’. In the child

  • 0

I have a class first where $name is set to ‘bob’. In the child class, I set $name to ‘Karen’ but it doesn’t work.

In my echo statements, the 1st one says “bob” instead of ‘Karen’. The second one, using a child class method, works though.

Why is this behavior?

class First {

    public $name;

    public function __construct() {

        $this->name = 'bob';
    }
}

class Third extends First {

    public $name = 'Karen';
    public function set_name ($name) {
        $this->name = $name;
    }
}

$instance_of_third = new Third;
$third_name = $instance_of_third->name;

echo "<br />We're looking for Karen: $third_name<br />";
// $third_name here is 'bob' from parent class

$instance_of_third->set_name("Karen");
$third_name = $instance_of_third->name;
// $third_name here is 'Karen' only after using set_name()
echo "<br />We're looking for Karen: $third_name<br />";

EDIT: I added 2 lines showing what the output was exactly.

Even though $name is explicitly set to ‘Karen’ in child class, it shows as ‘bob’ unless the set_name() function is used to change it.

  • 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-29T15:02:34+00:00Added an answer on May 29, 2026 at 3:02 pm

    Because your Third does not have it’s own constructor, it will inherit the constructor from First. Inheritance creates an behaves-as relationship between supertype and subtype.

    So when you create a new instance of Third, the instance will have a $name property of ‘Karen’, but then the inherited constructor will get called. And that sets the name to Bob. If you don’t want that behavior in the subtype, you have to add an empty constructor to Third, e.g.

    class Third extends First
    {
        public function __construct()
        {
            // prevents parent First::__construct
        }
    }
    

    demo

    An alternative would be to remove the ctor (or at least not set $name in it) and preset the $name property in the parent to Bob. Then your initial idea would work, e.g.

    class First
    {
        public $name = 'Bob';
    }
    
    class Third extends First
    {
        public $name = 'Karen';
    }
    

    demo

    Please check the chapters on Inheritance and Visibility in the PHP Manual:

    • http://php.net/manual/en/language.oop5.inheritance.php
    • http://php.net/manual/en/language.oop5.visibility.php
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Student with 3 attributes (first name, last name, age). In
I have a class that opens text files to grab first name, last name,
When you have an HTML element with no class name set, what is the
I have an entity class User that contains information such as username, first name,
I have 2 classes. public class Name { public string FirstName { get; set;
I have the following classes: public class Person { public String FirstName { set;
I have a class Person and a class Name. Name contains two Strings firstName
I have this sample model working with the admin class Author(models.Model): name = models.CharField(_('Text
Let's say I have a POCO like so: public class Name { public string
EF Code First - Model I have 2 classes: public class Alias { public

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.