PHP Classes

dynClass: Extends stdClass to have callable functions

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
StarStarStarStar 66%Total: 161 All time: 8,955 This week: 49Up
Version License PHP version Categories
dynclass 1.0.0GNU General Publi...5PHP 5, Data types
Description 

Author

This class can extends stdClass to have callable functions.

The __construct function takes an array of values that define either class variables or functions. The variables or functions may also be assigned setting an object value.

Calling a property assigned to a callable function will call the function forwarding any parameters.

Innovation Award
PHP Programming Innovation award nominee
April 2017
Number 7
stdClass is an empty base class built-in PHP that does not contain any functions or variables. It is used to create generic objects.

stdClass objects can be created by converting values of other types to objects. However there are limitations.

The dynClass overcomes some of stdClass limitations by allowing for instance to have assign callable functions to object via the class constructor that takes an array of properties and functions as parameter.

Manuel Lemos
Picture of Chris Jeffries
  Performance   Level  
Name: Chris Jeffries <contact>
Classes: 6 packages by
Country: United Kingdom
Age: 76
All time rank: 2174100 in United Kingdom
Week rank: 273 Up12 in United Kingdom Up
Innovation award
Innovation award
Nominee: 3x

Example

<?php
//test of dynclass vs stdClass
include "dynClass.class.php";
echo
'<h1>Initialising classes</h1>';
$dyn = new dynClass(array("z"=>"foo", "method1"=>function(){ return "Method1 called";}));
$std = new stdClass(array("z"=>"foo"));

echo
'<p>dynClass understands initialization from array</p>';
echo
"dyn ->z : " . $dyn->z;
echo
'<p>But stdClass does not</p>';
echo
"std ->z : " . $std->z;

$dyn->a = array(1,2,3,4,"ttt");
$std->a = array(1,2,3,4,"ttt");

$dyn->b = true;
$std->b = true;

$dyn->c = "string";
$std->c = "string";

$testclass = new stdClass();
$testclass->property = "test property";

$dyn->o = $testclass;
$std->o = $testclass;

$dyn->f = function($parm='no value passed') { return "lambda passed $parm"; } ;
$std->f = function($parm='no value passed') { return "lambda passed $parm"; } ;

$u = function($parm='no value passed') { return "Saved lambda returned $parm"; } ;
$dyn->u = $u;
$std->u = $u;

$t = function($parm='z') {
    return
$this->$parm;
    } ;
$dyn->t = $t;

global
$glob;
$glob = "This is a global";
$g = function () {
    global
$glob;
    return
"This is from outside: $glob";
     } ;

$dyn->g = $g;
$std->g = $g;

$dyn->i = 55;
$std->i = 55;

$dyn->e = 55E6;
$std->e = 55E6;

echo
"<h1>Var_dump of stdClass</h1><pre>";
var_dump($std);

echo
"</pre>var_dump of dynClass</h1><pre>";
var_dump($dyn);

echo
"
</pre><h1>Test lambdas added to dynClass as properties</h1><pre>
directly assigned lambda f :
{$dyn->f}
indirectly assigned lambda u :
{$dyn->u}
lambda referencing global g :
{$dyn->g}
lambda referencing \$this t :
{$dyn->t}
lambda at instantiation method1:
{$dyn->method1}
</pre>"
;

echo
"
</pre><h1>Test lambdas added to dynClass as methods</h1><pre>
directly assigned lambda f :
{$dyn->f("-value-")}
indirectly assigned lambda u :
{$dyn->u("-value-")}
lambda referencing global g :
{$dyn->g("-value-")}
lambda referencing \$this t :
{$dyn->t("z")}
</pre><h1>The remainder are going to fail</h1>"
;
try {
    echo
"</pre><h1>Test lambdas added to stdClass as properties</h1><pre>
    f :
{$std->f}
    u :
{$std->u}
    g :
{$std->g}
    </pre>"
;

    echo
"
    </pre><h1>Test lambdas added to stdClass as methods</h1><pre>
    f :
{$std->f("-value-")}
    u :
{$std->u("-value-")}
    g :
{$std->g("-value-")}
    </pre>"
;
}
catch (
Exception $e) {
    echo
"<br>As anticipated, this error occurred: $e";
}
?>


  Files folder image Files (3)  
File Role Description
Plain text file dynClass.class.php Class The class definition
Accessible without login Plain text file dynClass.docs.pdf Doc. How to use dynClass
Accessible without login Plain text file testdynclass.php Example Test program for dynClass

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
 Version Control Unique User Downloads Download Rankings  
 0%
Total:161
This week:0
All time:8,955
This week:49Up
 User Ratings  
 
 All time
Utility:81%StarStarStarStarStar
Consistency:87%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:87%StarStarStarStarStar
Tests:-
Videos:-
Overall:66%StarStarStarStar
Rank:484