PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Aleksandar Zivanovic  >  PHP Object Factory  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Class source
Class: PHP Object Factory
Create objects of given types defined by name
Author: By
Last change:
Date: 1 year ago
Size: 1,643 bytes
 

Contents

Class file image Download
<?php

require_once __DIR__ . '/vendor/autoload.php';

interface
IModel {}
abstract class
BaseModel implements IModel{}

interface
IUserModel extends IModel {}
class
UserModel extends BaseModel implements IUserModel {}

interface
IAccountModel extends IModel {
    public function
getUser(): IUserModel;
}
class
AccountModel extends BaseModel implements IAccountModel {
    private
IUserModel $user;

    public function
__construct(IUserModel $user)
    {
       
$this->user = $user;
    }

    public function
getUser(): IUserModel
   
{
        return
$this->user;
    }
}

interface
ICharacterAccount extends IAccountModel {}
class
CharacterAccountModel extends AccountModel implements ICharacterAccount {
    private
IAccountModel $userAccount;

    public function
__construct(IAccountModel $userAccount)
    {
       
parent::__construct($userAccount->getUser());

       
$this->userAccount = $userAccount;
    }
}

\
ObjectFactory\Factory::registerInterfaceClass(IUserModel::class, UserModel::class);
\
ObjectFactory\Factory::registerInterfaceInstanceProvider(IAccountModel::class, function (): IAccountModel {
    return \
ObjectFactory\Factory::getInstance(AccountModel::class);
});
\
ObjectFactory\Factory::registerInterfaceClass(ICharacterAccount::class, CharacterAccountModel::class);

$nonShared = \ObjectFactory\Factory::getInstance(AccountModel::class);
$sharedA = \ObjectFactory\Factory::getSharedInstance(AccountModel::class);
$sharedB = \ObjectFactory\Factory::getSharedInstance(AccountModel::class);
\
ObjectFactory\Factory::getInstance(CharacterAccountModel::class);

var_dump($nonShared === $sharedA, $sharedA === $sharedB);

For more information send a message to info at phpclasses dot org.