1

My class structure is as follows,

Class Core{
      public $Variable = "Test";

      Class SubClass{
            // functions, etc

      }

      // functions etc
}

I need to access the variable $Variable from within the SubClass class, but I cannot think of a way to do it. I have tried $this->this->Variable without success.

Edit While this is incorrect syntax, this is how my class system is setup (and is achieved using includes).

5
  • You gotta inject the parent class into the child class in the constructor or a factory class Commented Jul 23, 2013 at 21:23
  • lol I thought this was java for a sec... yea PHP doesn't support inner classes... and if it doesn't error out, then it's an intended "feature" Commented Jul 23, 2013 at 21:24
  • There is no such class-within-a-class construct in PHP, how is this supposed to work? Commented Jul 23, 2013 at 21:30
  • Edited the original question to add a bit more clarification. Commented Jul 23, 2013 at 22:11
  • You class system cannot be set up this way because it makes no sense. PHP does not have nested classes, period. If you include one class within another that still does't make them related in any way, so you cannot do what you want to do. Commented Jul 24, 2013 at 6:08

2 Answers 2

2

Assuming you had a proper inheritance model set up, you could use parent::. But your code as-is is a flat-out syntax error. You cannot nest classes like that.

Class Core {
   public $var = 'test';
}

Class SubClass Extends Core {
   function foo() {
      $localvar = parent::$var;
   }
}

comment followup:

Perhaps something more like this?

class Core {
    public $Variable = 'foo';
    function __construct() {
       $this->subclass = new SubClass($this->variable);
    }
}

class SubClass {
    public $coreVariable;
    function __construct(&$var) {
       $this->coreVariable = $var;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Edited the original question to add a bit more clarification.
what you want is not possible with PHP. you could instantiate SubClass inside Core's constructor, and pass Core::Variable in as a reference to SubClass's constructor.
0

I am going to answer this because the previous comments show so much ignorance about how PHP works and the scope of variables when nesting Classes and functions which is perfectly fine to do in PHP. It happens a lot when using third party classes as includes in procedural code bases.

Class Core{
      public $Variable = "Test";

      Class SubClass{
            // functions, etc
            function new()
            {
               global $Variable;// this brings the variable into scope
               echo $Variable;
            {



      }

      // functions etc
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.