PHP Classes

File: Falcraft/examples/Patterns/Prototype.php

Recommend this page to a friend!
  Packages of Asher Wolfstein   JavaScript Like Prototypes   Falcraft/examples/Patterns/Prototype.php   Download  
File: Falcraft/examples/Patterns/Prototype.php
Role: Example script
Content type: text/plain
Description: Example Script
Class: JavaScript Like Prototypes
Implement class prototypes similar to JavaScript
Author: By
Last change:
Date: 11 years ago
Size: 2,484 bytes
 

Contents

Class file image Download
<?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";
}