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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:40:04+00:00 2026-05-23T23:40:04+00:00

Hi stackOverflow Family :), I have a question, and I didt find the answer

  • 0

Hi stackOverflow Family :),

I have a question, and I didt find the answer elsewhere. I try to explain my problem:
I have a class, and if I create an other class from it, from that child class I couldnt access the parent’s properties.
I did something wrong?
I tried to copy my class variable to a local and try to give back that local one, but neither works of the following 3 way.

Here is my examples.
At first I simple create an object:

$test = new test();

And my two class is the following:

class test {

    public $testvar;

    public function __construct() {
        $this->testvar = 1234568;
        echo ":) ".$this->testvar();
        $test2 = new test2();
}

    public function testvar() {
        echo "testvar() called > ";
        return $this->testvar;
    }
}

And test2:

class test2 extends test  {

    public function __construct() {
        echo "<br>:| this-> ".$this->testvar;
        echo "<br>:| parent:: ". parent::testvar();
        echo "<br>:| "; $this->testvar();
    }

}

May somebody have an idea?
Thx

  • 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-23T23:40:05+00:00Added an answer on May 23, 2026 at 11:40 pm

    You’ve misunderstood the inheritance concept. Instantiating test2 in the constructor function of test is not inheritance.

    The constructor of test was not called, therefore testvar was not set. Remove $test2 = new test2(); from the constructor of test. Try:

    class test {
    
        public $testvar;
    
        public function __construct() {
            $this->testvar = 1234568;
            echo ":) ".$this->testvar();
    }
    
        public function testvar() {
            echo "testvar() called > ";
            return $this->testvar;
        }
    }
    
    class test2 extends test  {
    
        public function __construct() {
            parent::__construct();
            echo "<br>:| this-> ".$this->testvar;
            echo "<br>:| "; $this->testvar();
        }
    
    }
    
    $test2 = new test2();
    

    See also the PHP manual on constructors (and classes too).

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

Sidebar

Related Questions

Do class objects declared on the stack have the same lifetime as other stack
Hi Stackoverflow people, in my current project, I have the following model structure: class
Many StackOverflow posts and other online forums have questions similar to this, but none
Hello fellow stackoverflow family members? I know it is un-efficient to create one extra
Stackoverflow User Luke wrote in this answer : The boundaries between desktop and web
StackOverflow, you're my only hope! I am unfortunate enough to have inherited an ASP.NET
In StackOverflow podcast no. 19 , Joe describe Fogcreek's decision to have one database
This StackOverflow answer has an image of KDiff3 highlighting intra-line differences. Does someone know
I don't normally go to stackoverflow for sigsegv errors, but I have done all
I have started this support ticket project where users can create tickets and the

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.