0

I have a class like this:

class A {

 public $var = "";

 function __construct() {

  $this->var = "value";

 }

}

And a child class like this:

class B extends A {

 function __construct() {

  // Is this correct?
  parent::__construct();

 }

 function my_function() {

  // Or this?
  // $options is an instantiation of A.
  global $options;

  echo $this->var;

 }

}

The problem I was having is that when I called the my_function() method, the value of var was empty. After reading on php.net for a while I found out that when a child class has its own constructor, the parent constructor is overridden which is why my variable was empty. My question is if the way I'm calling parent::__construct() is the right solution or if I should just globalize the instantiated object that I created in my script? I've done a lot of reading in comments on PHP.net and other places and I couldn't find anything concise.

1
  • I took your exact class definitions, instantiated B and called my_function, and value was printed. Is this not correct? Commented Apr 5, 2011 at 16:39

2 Answers 2

1

With parent::method() you call the overridden method (not only the constructor), so your solution is the right one. In your case you can omit the constructor completely and just set the value, when you declare the property.

class A {
  public $var = "value";
}

Additional: Global variables are ugly in every way. Use them only, if you have really good reasons to do so and never, because its more convenient.

Sign up to request clarification or add additional context in comments.

1 Comment

The reason I'm setting that variable from A's constructor is that I do some setting of variables there. I have a loader file which I include into every page. In the loader file is my config include. The A class reads the config file and sets several variables in A. In my loader file I have an instantiation of A which I use throughout pages to access those variable that were set in A.
0

The following "implementation" ...

$instanceOfB = new B();
$instanceOfB->my_function();

results in "value" ... as excpected.

What has been your way of calling my_function() ?

2 Comments

I know it's working. My question is whether or not what I am doing is correct. I already have instantiated the object in my script like this: $options = new A. I just wasn't sure if called parent::_construct() in the in the child class constructor was ok since I would be calling the A::__construct() method twice in my script ( once with $options then again in the child constructor ).
Ah, I see. 1st - I'd recommend to declare A::$var as protected, if you intend to acces this var by child classes only. Public would allow any included script to modify $var's value. 2nd - the constructor of A is not called twice, if you instantiate $options as new B();. Try printing something in A's constructor ... it will be printed. Remove parent::__construct() in B's constructor: nothing happens. You don't need to access a GLOBAL $options in my_function(). Just make use of inherited A ...

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.