Skip to content
jeanphilippe-p edited this page Aug 13, 2016 · 31 revisions

Table of content

  1. [How it works ?] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#how-it-works-)
  2. [Model class] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#model-class)
  3. [Simple Model] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#simple-model)
  4. [Complex Model] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#complex-model)
  5. [Container Model] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#container-model)
  6. [InstanceModel class] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#instancemodel-class)
  7. [Object class] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#object-class)
  8. [Extends from Object] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#extends-from-object)
  9. [Code Example] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Getting-started#code-example)

How it works ?

[Comhon!] () has two main concepts to understand : object and model.

  • An Object contain values and a model
  • Values is a map of values [a_property_name => a_value]
  • A Model contain properties
  • A property has a name and a type
  • A type can be (string, boolean, another model ...)

In other words map of values replace class attributes and model properties replace attributes types.

Model class

Simple Model

list of simple model managed :

  • boolean
  • dateTime
  • float
  • integer
  • string

Complex Model

A complex model is described by a manifest (please take a look at [Manifest] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Manifest) page). It contain a map of properties and each property has it own model

Container Model

Model can associate a container to a property name and this container will contain the "real" model.

For exemple you have a model Person and you want to have a property sex and this property can have only 2 values : male or female (enumeration). So your property sex will have a container ModelEnum that contain a model String.

Properties can have three kind of container :

  • ModelForeign (property is a reference to another object)
  • ModelEnum (enumeration)
  • ModelArray (array)

InstanceModel class

Instanciating models has a cost (due to manifest loading) so we need to instanciate them only one time. The singleton InstanceModel manage all models instances so we don't have to know if model is already instanciated or not.

To get an instance of a model you just have to call getInstanceModel() :

$model = InstanceModel::getInstance()->getInstanceModel('a_model_name');

Object class

Like we said before, an Object contain a Model so when you instanciate an Object you have to indicate it's model.

$object = new Object('a_model_name');

you can also use model and call function getObjectInstance().

$object = $model->getObjectInstance();

To access value you have to call getter

$object->getValue('property_name');

To set a value you have to call setter

$object->setValue('property_name', a_value);

Extends from Object

if you want to associate a class to your object you can create one and define it in your manifest (see [Manifest] (https://github.com/jeanphilippe-p/ObjectManagerLib/wiki/Manifest) page). Your class must extends from Object class

class Person Extends Object {
  public function getName() {
    return $this->getValue('name');
  }
  
  public function setAge($age) {
    $this->setValue('age', $age);
  }
}

Now to get an instance of Person you can use model and call function getInstanceModel().

$personModel = InstanceModel::getInstance()->getInstanceModel('Person');
$person = $personModel->getObjectInstance();

Code Example

// first way to instanciate an object
$personModel = InstanceModel::getInstance()->getInstanceModel('person');
$person = $personModel->getObjectInstance();

// second way to instanciate an object
$person = new Object('person');

// set object values
$person->setValue('age', 21);
$person->setValue('foo', 'bar'); // will not work because person doesn't have property 'foo'

// get an object value
$age = $person->getValue('age');

Clone this wiki locally