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