AAD Web Team Posted Friday at 01:41 AM Share Posted Friday at 01:41 AM If we’re using custom page classes, is it acceptable to have a constructor, or does that cause problems by overriding the constructor that should be running in the parent Page class? Should we call the parent class constructor? For example in /site/classes/DefaultPage.php, would this be correct? class DefaultPage extends Page { function __construct() { parent::__construct(); // … set some class properties for later use … } } Link to comment Share on other sites More sharing options...
szabesz Posted Friday at 07:39 AM Share Posted Friday at 07:39 AM ProcessWire's Page constructor: https://github.com/processwire/processwire/blob/3cc76cc886a49313b4bfb9a1a904bd88d11b7cb7/wire/core/Page.php#L594 public function __construct(Template $tpl = null) { parent::__construct(); if($tpl !== null) { $tpl->wire($this); $this->template = $tpl; } $this->useFuel(false); // prevent fuel from being in local scope $this->parentPrevious = null; $this->templatePrevious = null; $this->statusPrevious = null; } So in extending custom page calsses I do this: function __construct(Template $tpl = null) { parent::__construct($tpl); // my initializations go here... } I had no issues with it so far. 3 Link to comment Share on other sites More sharing options...
AAD Web Team Posted Monday at 11:17 PM Author Share Posted Monday at 11:17 PM Great, thanks for that! I’ve implemented a constructor and it seems fine. I initially made a mistake by including a reference to a page that has no view of its own, but contains some general website settings, like this: public function __construct(Template $tpl = null) { parent::__construct($tpl); $this->siteSettings = pages('/site-settings/'); } This caused an infinite loop because the site-settings page itself uses the DefaultPage class. To get around this I created an empty class for the site-settings template, like this: class SiteSettingsPage extends Page { } I’m not sure if this was the smartest solution, or if the idea of having so much code in DefaultPage is a good idea. I wonder if there are a heap of system pages and other things that I’m not thinking of, which are inheriting from DefaultPage? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now