Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gabrieljmj/prototype
This library allows you to create prototypes like in JavaScript
https://github.com/gabrieljmj/prototype
Last synced: about 2 months ago
JSON representation
This library allows you to create prototypes like in JavaScript
- Host: GitHub
- URL: https://github.com/gabrieljmj/prototype
- Owner: gabrieljmj
- License: other
- Created: 2014-10-25T21:42:56.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-01T01:32:15.000Z (about 10 years ago)
- Last Synced: 2024-05-05T13:21:40.500Z (8 months ago)
- Language: PHP
- Homepage:
- Size: 169 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Gabrieljmj\Prototype
====================
[![Total Downloads](https://poser.pugx.org/gabrieljmj/prototype/downloads.png)](https://packagist.org/packages/gabrieljmj/prototype) [![Latest Unstable Version](https://poser.pugx.org/gabrieljmj/prototype/v/unstable.png)](https://packagist.org/packages/gabrieljmj/prototype) [![License](https://poser.pugx.org/gabrieljmj/prototype/license.png)](https://packagist.org/packages/gabrieljmj/prototype) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/GabrielJMJ/Prototype/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/GabrielJMJ/Prototype/?branch=master)This library allows you to create prototypes almost like in JavaScript.
##Tests
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.##1. Creating an object
Objects are created with functions. These functions will return an object. All objects are registred on a class called ```\Gabrieljmj\Prototype\Prototype```.
```php
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:
```php
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:
```php
$user1 = new Person();
$user1->setName('Hansel');$user2 = new Person();
$user2->setName('Gretel');echo $user1->getName() . ' and ' . $user2->getName(); //Hansel and Gretel
```
##2. Extending
The extending is almost the same of JavaScript. Just set the property ```$prototype```:
```php
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.
```
###2.1. Instanceof
```php
var_dump($person instanceof Employee); //bool(false)
var_dump($employee instanceof Person); //bool(true)
```