MageWork

Back to home

Objects and class fallback

Custom classes

You can use a custom class for any types, by specifying the wanted class name.

<?php
// packages/Acme/etc/config.php

$config = [
    Core_Page::TYPE => [ // Type
        'contact' => [ // Identifier
            'class' => Acme_Page_Contact::class,
        ],
    ],
    Core_Block::TYPE => [ // Type
        'banner' => [ // Identifier
            'class' => Acme_Block_Banner::class,
        ],
    ],
    Core_Model::TYPE => [ // Type
        'customer' => [ // Identifier
            'class' => Acme_Model_Customer::class,
        ],
    ],
];

To create an instance of an object, you need to use the App::getSingleton or App::getObject methods:

App::getSingleton({identifier}, {type}, {package}) (Create a single instance)

App::getObject({identifier}, {type}, {package}) (Create a new instance)

<?php

/** @var Acme_Model_Customer $model */
$model = App::getSingleton('customer', Core_Model::TYPE);
// packages/Acme/Model/Customer.php

/** @var Admin_Model_Customer $model */
$model = App::getSingleton('customer', Core_Model::TYPE, 'admin'); 
// packages/Admin/Model/Customer.php

Class fallback priorities

If the class is missing, the system will automatically attempt to load a class based on the following priorities:

  1. {package}_{type}_{identifier}
  2. Core_{type}_{identifier}
  3. {package}_{type}
  4. Core_{type}
  5. DataObject

Overrides

To override default core objects like Core_Block or Core_Page, add the class to the root of your package:

packages > Acme > Page.php

<?php

declare(strict_types=1);

class Acme_Page extends Core_Page
{
    public function myCustomMethod(): string
    {
        return 'Hello World!';
    }
}

Your class methods will now be available for all pages.

Don't forget to inherit your own classes from Acme_Page instead of Core_Page.

In the same way for the blocks:

packages > Acme > Block.php

<?php

declare(strict_types=1);

class Acme_Page extends Core_Block
{
    public function myCustomMethod(): string
    {
        return 'Hello World!';
    }
}

Your class methods will now be available for all blocks.

Fallback examples

For a block type, with banner identifier, in the Acme package.

App::getSingleton('banner', Core_Block::TYPE)

The system will try to load classes in this order until it is found:

  1. Acme_Block_Banner
  2. Core_Block_Banner
  3. Acme_Block
  4. Core_Block
  5. DataObject

For a model type, with customer identifier, in the Acme package.

App::getSingleton('customer', Core_Model::TYPE)

The system will try to load classes in this order until it is found:

  1. Acme_Model_Customer
  2. Core_Model_Customer
  3. Acme_Customer
  4. Core_Customer
  5. DataObject