Jump to content

How to add virtual field to Page object?


lehtu
 Share

Recommended Posts

Hi,

I have this situation I'm working on my template file and $page is what I want.. But I just want to add one field to it. And there is more.. I don't want to save it. So I just want to add new $field to my $page object virtually. After this I'm going to change it to JSON and print it out.

Is this possible? If yes, please share the answer :)

Thanks!

Link to comment
Share on other sites

If the field your settings a value for is not reserved or found in the templates fields it's just added to the object as runtime property.  

$page->notAAssingedField = "Hello";

echo $page->notAAssingedField; // outputs: Hello

If you'd need more specific additions you could also use hooks to extend the page object or even make a own object, extending the existing page object. This new object can then be assinged to specific templates in the backend. 

Edit:

As you've said you'll export it to JSON: A page object has lots of properties, which I don't know if you need all of them. So I would suggest taking a look at the json so it's not bloated with stuff you're not using, especially if it's for something like ajax.

  • Like 2
Link to comment
Share on other sites

Unless am mistaken, this is called overloading. In PHP, you can overload both properties and methods. 

Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.

http://php.net/manual/en/language.oop5.overloading.php

http://phppot.com/php/overloading-in-php/

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...