PHP code example of comhon-project / comhon

1. Go to this page and download the library: Download comhon-project/comhon library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

comhon-project / comhon example snippets


// first way to instanciate a comhon object
$personModel = InstanceModel::getInstance()->getInstanceModel('Example\Person');
$person = $personModel->getObjectInstance();

// second way to instanciate a comhon object
$person = new ComhonObject('Example\Person');

// third way to instanciate a comhon object only if you have defined a class
$person = new Person();

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

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

// instanciate a comhon object by importing json
$interfacer = new StdObjectInterfacer();
$person = $interfacer->import(json_decode('{"first_name":"john","last_name":"john","age":21}'), $personModel);

// fill an existing comhon object by importing json
$interfacer = new StdObjectInterfacer();
$person->fill(json_decode('{"first_name":"john","last_name":"john","age":21}'), $interfacer);

// export a comhon object to xml and print it as string
$interfacer = new XMLInterfacer();
$nodeXML = $interfacer->export($person);
echo $interfacer->toString($nodeXML);
// output : <root first_name="john" last_name="john" age=21 />

// load, update and save an object (from/to sql database or xml file or json file)
$loadedPerson = $personModel->loadObject('id_of_a_person');
$loadedPerson->setValue('age', 25);
$loadedPerson->save();