PHP code example of jameslevi / objectify

1. Go to this page and download the library: Download jameslevi/objectify 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/ */

    

jameslevi / objectify example snippets




if(file_exists(__DIR__.'/vendor/autoload.php'))
{
    



use Graphite\Component\Objectify\Objectify;

$data = new Objectify(array(
  'x' => 0,
  'y' => 0,
  'z' => 1,
));

echo $data->get("x"); // Will echo the value of x.

echo $data->x; // Will echo the value of x.

$data->set("x", 100); // Set the new value of x.

$data->x = 200; // Set the new value of x.

$data->add("pi", 3.14);

$data->has("x") // Returns true.

$data->remove("z"); // This will remove z from the data object.

$data->keys(); // This will return all data object keys.

$data->toArray(); // This will return all the data from the data object in array.

$data->toJson(); // This will return json formatted string.



use Graphite\Component\Objectify\Objectify;

class Car extends Objectify
{
    public function setDriver($name)
    {
        $this->driver = "Mr. " . ucwords($name);
    }
}

$car = new Car(array(
    'manufacturer'          => 'Honda',
    'model'                 => 'Civic',
    'type'                  => 'Sedan',
    'driver'                => null,
));

$car->setDriver("james crisostomo");

echo $car->driver; // Result will be "Mr. James Crisostomo".

$data = new Objectify(["x" => 0], true);

$data->set("y", 1); // Updating value is disabled.
$data->add("y", 1); // Adding new data is disabled.
$data->remove("x"); // Removing data is disabled.

$new_data = $data->clone(); // This will create an exact copy of the data object.