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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:27:49+00:00 2026-06-09T02:27:49+00:00

In the Child class, I’d need to access the parent property $this->lang and use

  • 0

In the Child class, I’d need to access the parent property “$this->lang” and use it for other properties, before calling the parent constructor again, like :

abstract class mySuperClass {

    protected $lang;

    public function __construct($arg1=null, $arg2=null, $arg3=null) {
        $this->lang = "US";
    }
}

class myChild extends mySuperClass {

    public function __construct($arg1=null, $arg2=null, $arg3=null) {
        $new_arg1 = $this->lang; // HERE WE ARE STRUGGLYING ...
        echo "<br/>step 1) ". get_class(). " : lang= $new_arg1 "; // returns "" where I would need "US"
        parent::__construct($new_arg1 , $arg2, $arg3) ;
        echo "<br/>step 2) ". get_class(). " : lang= $this->lang"; // returns "US"

    }
}

$obj = new myChild("foo1", "foo2", "foo3") ;

This does not look possible/allowed as the parent class is not instanciated at this stage where we are trying to call the parent property $this->lang. Which seems logical.

So the question is :
Is there a way to access the parent property in the child class, with a constructor override as shown ?

  • 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-09T02:27:51+00:00Added an answer on June 9, 2026 at 2:27 am

    If you are statically setting these things anyway (i.e. using hard-coded strings), then make it (the default at least) a static property (possibly even a class constant). Then reference the parent’s static var in the child’s constructor (and in the parent’s, too)

    abstract class mySuperClass {
        static protected $default_lang = "US";    
        protected $lang;
    
        public function __construct($arg1=null, $arg2=null, $arg3=null) {
            $this->lang = self::$default_lang;
        }
    }
    
    class myChild extends mySuperClass {
    
        public function __construct($arg1=null, $arg2=null, $arg3=null) {
            $new_arg1 = parent::$default_lang; 
            echo "<br/>step 1) ". get_class(). " : lang= $new_arg1 "; 
            parent::__construct($new_arg1 , $arg2, $arg3) ;
            echo "<br/>step 2) ". get_class(). " : lang= $this->lang"; // returns "US"
    
        }
    }
    
    $obj = new myChild("foo1", "foo2", "foo3") ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does this child class access the parent class within livewallpaper service. public class
Given a simple parent/child class structure. I want to use linqkit to apply a
I have a parent and child class that both need to implement IDisposable .
I am trying to access child class variables in parent class..can u suggest me
A child class can access protected functions in parent class,but the parent class can't
I got this example Parent class Vehicle Child Classes Car , Motorcycle , &
I'm attempting to access member variables in a child class via the parent class
Is it possible to use the initialization list of a child class' constructor to
I'm trying to access protected variables in a second-child class extended from its parent,
Why Child class doesn't have echo() method? Parent = function(){ this.name = 'abc'; }

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.