PHP code example of icanboogie / prototype

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

    

icanboogie / prototype example snippets




use ICanBoogie\Prototype;
use ICanBoogie\PrototypeTrait;

class Cat { use PrototypeTrait; }
class OtherCat extends Cat {}
class FierceCat extends Cat {}

$cat = new Cat;
$other_cat = new OtherCat;
$fierce_cat = new FierceCat;
$second_fierce_cat = new FierceCat;

// define the 'meow' prototype method for Cat class
Prototype::from(Cat::class)['meow'] = fn(Cat $cat) => 'Meow';

// override the 'meow' prototype method for FierceCat class
Prototype::from(FierceCat::class)['meow'] = fn(Cat $cat) => 'MEOOOW !';

echo $cat->meow();               // Meow
echo $other_cat->meow();         // Meow
echo $fierce_cat->meow();        // MEOOOW !
echo $second_fierce_cat->meow(); // MEOOOW !



use ICanBoogie\Prototype;
use ICanBoogie\PrototypeTrait;

class TimeObject
{
	use PrototypeTrait;

	public $seconds;
}

$time = new Time;
$prototype = Prototype::from(Time::class);

$prototype['set_minutes'] = function(Time $time, $minutes) {

	$time->seconds = $minutes * 60;

};

$prototype['get_minutes'] = function(Time $time, $minutes) {

	return $time->seconds / 60;

};

$time->seconds = 120;
echo $time->minutes; // 2

$time->minutes = 4;
echo $time->seconds; // 240



use ICanBoogie\Prototype;
use ICanBoogie\PrototypeTrait;

class Article
{
	use PrototypeTrait;

	public $image_id;
}

// …

Prototype::from(Article::class)['get_image']
    = fn(Article $target) => $image_model[$target->image_id] ?? null;

$article = new Article;
$article->image_id = 12;
echo $article->image->nid; // 12



use ICanBoogie\Prototype;
use ICanBoogie\PrototypeTrait;

class Node
{
	use PrototypeTrait;
}

class News extends Node
{
	public function url($type)
	{
		return parent::url("another/$type");
	}
}

Prototype::from(Node::class)['url'] = fn(Node $node, string $type) => "/path/to/$type.html";

$node = new Node;
$news = new News;

echo $node->url('madonna'); // /path/to/madonna.html
echo $news->url('madonna'); // /path/to/another/madonna.html



namespace ICanBoogie;

use ICanBoogie\Prototype\ConfigBuilder;use ICanBoogie\PrototypeTest\FierceCat;

$config = (new ConfigBuilder())
    ->bind(Cat::class, 'meom', fn(Cat $cat) => 'Meow')
    ->bind(FierceCat::class, 'meow', fn(FierceCat $cat) => 'MEOOOW !')
    ->build();

ICanBoogie\Prototype::bind($config);



use ICanBoogie\PrototypeTrait;

class Cat
{
	use PrototypeTrait;
}

$cat = new Cat;

$cat->prototype['meow'] = fn(Cat $cat) => 'Meow';

echo $cat->meow();



use ICanBoogie\Prototype;

Prototype::from(Cat::class)['meow'] = fn(Cat $cat) => 'Meow';



use ICanBoogie\Prototyped;

class A extends Prototyped
{
	public $a;
	protected $b;
	private $c;

	public function __construct($a, $b, $c)
	{
		$this->a = $a;
		$this->b = $b;
		$this->c = $c;
	}

	protected function get_c()
	{
		return $this->c;
	}

	protected function set_c($value)
	{
		$this->c = $value;
	}
}

$a = new A('a', 'b', 'c');

var_dump($a->to_array());

// array(2) {
//  ["a"]=>
//  string(1) "a"
//  ["c"]=>
//  string(1) "c"
//}



echo $a->to_json(); // {"a":"a","c":"c"}



class A extends Prototyped
{
	private $a;
	protected $b;
	public $c;
}

$a = A::from([ 'b' => 2, 'c' => 3 ]);