質問

There are similar questions but none that are giving me a satisfactory answer.

First off, here's my (simplified) code:

class classA; {
  function __construct() {
    ...do something...
    $this->init();
  }
}

class classB extends classA {
  function init {
    ...do something specific to this class which has to be done in the constructor...
  }
}

When I call a new instance of the child function, the line "$this->init();" triggers the following error in the Apache error log:

[error] [client ip] PHP Parse error: syntax error, unexpected T_STRING, 
expecting T_FUNCTION in ... on line... referrer:...

My reasoning is that I have things that I need to do in the constructor of the child class but I can't have a constructor there otherwise the constructor of the parent won't be processed. So I create the child, which pulls in the functions from the parent and executes the __constructor function. This function calls the (child's) init() function which has the child-specific logic and continues execution.

My only guess is that it's calling a function that isn't read into memory yet - but I thought PHP read the entire file into memory before execution of the __constructor function. I've looked to see if there's a preload function but haven't found anything other than doing an include statement - but I feel this is the wrong track.

Any help is greatly appreciated!

EDIT: touching up some irrelevant details.

役に立ちましたか?

解決

Within your child class call the parents constructor

   class SubClass extends BaseClass {
   function __construct() {
       parent::__construct();
       print "In SubClass constructor\n";
   }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top