Overwrite $page object with custom values

I tried to create a page object and then replace the values in it with custom ones.

See the code below:

$page = page('home');

page::$methods['title'] = function($page) {
  return 'CUSTOM TITLE';
};

$page->title = 'CUSTOM TITLE';

echo $page->title();

It still says:

Home

It works for values that does not exists

$page = page('home');
page::$methods['title1'] = function($page) {
  return 'CUSTOM TITLE1';
};
$page->title1 = 'CUSTOM TITLE2';
echo $page->title1();

CUSTOM TITLE2

In this case it does not help me, as I need to overwrite them, not create new ones.

Is there a way to overwrite $page object values?

As stated in the docs, you cannot override default page methods. You probably can using a page model: https://getkirby.com/docs/developer-guide/advanced/models

1 Like