Jump to content

Page Classes Constructor Inheritance


Inxentas
 Share

Recommended Posts

I have a weird issue regarding page classes. For my module, I'm using a couple of those. For clarity, class names are in bold while template names are in italic.

  1. The WebshopPage class extends the Page class and is the class from which the others extend.
  2. PageCheckOutPage is the class for the CheckoutPage template and extends WebShopPage.

Now I've added the following constructor to WebshopPage, in order to make sure every page that extends it has scope on an instance of the (custom) Cart class:

public function __construct($page)
{
  $this->cart = new Cart($this->session);
  parent::__construct($page);
  var_dump( get_class($this) );
}

Now what happens is that the constructor of the Cart class is called twice when I view a page with the CheckoutPage template. Yet the PageCheckoutPage class doesn't have a constructor of it's own, so I would have expected it to fire it's base class constructor once. Yet with the var_dump above, I get the following result:

C:\wamp\www\webshop.com\public_html\site\classes\Cart.php:14:string 'Cart constructor' (length=16)
C:\wamp\www\webshop.com\public_html\site\classes\WebshopPage.php:16:string 'ProcessWire\PageCheckoutPage' (length=28)
C:\wamp\www\webshop.com\public_html\site\classes\Cart.php:14:string 'Cart constructor' (length=16)
C:\wamp\www\webshop.com\public_html\site\classes\WebshopPage.php:16:string 'ProcessWire\PageWebshopPage' (length=27)

Instead of the extended class simply using the base class constructor as I would have expected, both the base class as the extended class fire a constructor seperately. Now I have a hunch that this happens because each page's parent is also instantiated, and thus it's constructor is ran. I can tell from nesting pages and then using var_dump($page->name) instead.

Is this assumption correct? Perhaps there is a  better way to ensure that all pages that extend WebshopPage have scope on an instance of the Cart class? I'd rather not have this constructor run as many times as a given page has parents.

 

Link to comment
Share on other sites

Shouldn't the cart belong to $session or $user? You could of course make $cart a static property in your WebshopPage class to have a singleton of Cart and only instantiate it if it is still null.

Link to comment
Share on other sites

1 hour ago, BitPoet said:

Shouldn't the cart belong to $session or $user? You could of course make $cart a static property in your WebshopPage class to have a singleton of Cart and only instantiate it if it is still null.

Yeah I'm still debating where it belongs, I just thought it nice to encapsulate them into the page classes.

Why did I not think of using it as a static property? That seems a good idea.
That way I can still have the constructors be responsible for it's auto-instantiation on every page that extends WebshopPage ? but be sure there is only one instance. 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...