01/11
2014
Wallogit.com
2017 © Pedro Peláez
This library allows you to create prototypes almost like in JavaScript., (*2)
Sorry, I know that it is wrong, but I left the tests to do after the library. Why? Because I am having problems to write them., (*3)
Objects are created with functions. These functions will return an object. All objects are registred on a class called \Gabrieljmj\Prototype\Prototype., (*4)
require_once 'autoload.php';
use Gabrieljmj\Prototype\Prototype;
function Person() {
return Prototype::getInstance()->prot('Person');
}
To set methods, you need set as global the variable $self and use like you use $this on OOP:, (*5)
Person()->on = 1;
Person()->setName = function ($name) {
global $self;
$self->name = $name;
};
Person()->getName = function() {
global $self;
return $self->name;
};
So you can instance this and execute the methods and get the propeties:, (*6)
$user1 = new Person();
$user1->setName('Hansel');
$user2 = new Person();
$user2->setName('Gretel');
echo $user1->getName() . ' and ' . $user2->getName(); //Hansel and Gretel
The extending is almost the same of JavaScript. Just set the property $prototype:, (*7)
function Employee() {
return Prototype::getInstance()->prot('Employee');
}
Employee()->prototype = new Person();
Employee()->setJobTitle = function ($jobtitle) {
global $self;
$self->jobTitle = $jobtitle;
};
Employee()->getJobTitle = function () {
global $self;
return $self->jobTitle;
};
$employee = new Employee();
$employee->setName('Jhon');
$employee->setJobTitle('Developer');
echo 'Hi! My name is ' . $employee->getName() . ' and I work as ' . $employee->getJobTitle() . '.';
//Hi! My name is Jhon and I work as Developer.
var_dump($person instanceof Employee); //bool(false) var_dump($employee instanceof Person); //bool(true)