PHP Classes

JavaScript Like Prototypes: Implement class prototypes similar to JavaScript

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 199 All time: 8,492 This week: 49Up
Version License PHP version Categories
falcraftprototypes 1.0MIT/X Consortium ...5.3PHP 5, Data types, Language
Description 

Author

This class implement class prototypes similar to JavaScript.

It can set classes functions as function closures and variables to given values.

New prototype classes can inherit the prototype definitions of base classes.

Innovation Award
PHP Programming Innovation award nominee
December 2015
Number 8
JavaScript implements a different object model based on prototypes for functions and variables that are made available to all objects that use the same prototypes.

This package implements a solution that can work in PHP like JavaScript prototypes.

Prototype functions use dynamically assigned PHP function closures. It also supports prototype inheriting for creating new object prototypes.

Manuel Lemos
Picture of Asher Wolfstein
  Performance   Level  
Name: Asher Wolfstein <contact>
Classes: 15 packages by
Country: United States United States
Age: 41
All time rank: 1121164 in United States United States
Week rank: 178 Up22 in United States United States Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php

require_once('../../Patterns/Prototype.php');

use
Falcraft\Patterns;

echo
"Falcraft\\Patterns\\Prototype.php Test\n";
echo
"------------------------------------\n\n";

echo
"Instantiating Prototype -> ";

$success = true;

$prototype1 = null;

try {
   
$prototype1 = new Patterns\Prototype();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Retrieve Identity and Register -> ";

$success = true;

$prototype1Identity = '';

try {
   
$prototype1Identity = $prototype1->getIdentity();
   
$prototype1->register();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

echo
"Define Properties On Base Prototype -> ";

$success = true;

try {
   
$prototype1->testFunction = function () {echo "Inside Test Function\n";};
   
$prototype1->testMemberVariable = "Test Member Variable\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Get Prototype Using Base as Parent -> ";

$success = true;

$prototype2 = null;

try {
   
$prototype2 = $prototype1->from();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Prototype Inherits Properties -> \n";

$success = true;

try {
    echo
' ';
   
$prototype2->testFunction();
    echo
' ';
    echo
$prototype2->testMemberVariable;
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

echo
"Prototype 2 'Redefines' testFunction -> \n";

$success = true;

try {
   
$prototype2->testFunction = function(){echo "Inside Test Function 2\n";};
    echo
' ';
   
$prototype2->testFunction();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

echo
"Instantiate Prototype 3 from Prototype 2,\n";
echo
"Define new Prototype 1 Function,\n";
echo
"Call new function from prototype 3 => ";

$success = true;

try {
   
$prototype3 = new Patterns\Prototype();
   
$prototype3->__parent = $prototype2;
   
$prototype1->newTestFunction = function(){echo"Inside New Test Function\n";};
   
$prototype3->newTestFunction();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}


  Files folder image Files (5)  
File Role Description
Files folder imageFalcraft (1 file, 2 directories)

  Files folder image Files (5)  /  Falcraft  
File Role Description
Files folder imageexamples (1 directory)
Files folder imagePatterns (1 file, 1 directory)
  Accessible without login Plain text file falcraftLoad.php Aux. Loader Abstraction

  Files folder image Files (5)  /  Falcraft  /  examples  
File Role Description
Files folder imagePatterns (1 file)

  Files folder image Files (5)  /  Falcraft  /  examples  /  Patterns  
File Role Description
  Accessible without login Plain text file Prototype.php Example Example Script

  Files folder image Files (5)  /  Falcraft  /  Patterns  
File Role Description
Files folder imageException (2 files)
  Plain text file Prototype.php Class Main Class

  Files folder image Files (5)  /  Falcraft  /  Patterns  /  Exception  
File Role Description
  Plain text file ExceptionInterface.php Class Exception Interface
  Plain text file RuntimeException.php Class Local Exception Class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadfalcraftprototypes-2015-12-06.zip 6KB
Downloadfalcraftprototypes-2015-12-06.tar.gz 4KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
PHP Configuration Trait Download .zip .tar.gz Configuration of Class Required
Abstract Data Types Download .zip .tar.gz You Can Use Map As Registry Required
 Version Control Unique User Downloads Download Rankings  
 0%
Total:199
This week:0
All time:8,492
This week:49Up