PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Cesar D. Rodas   Dynamic Method   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: Dynamic Method
Dynamically add functions to objects
Author: By
Last change: * example
Date: 16 years ago
Size: 3,052 bytes
 

Contents

Class file image Download
<?php
/*
***************************************************************************
* Copyright (C) 2007 by Cesar D. Rodas *
* cesar@sixdegrees.com.br *
* *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to *
* the following conditions: *
* *
* The above copyright notice and this permission notice shall be *
* included in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR *
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
* OTHER DEALINGS IN THE SOFTWARE. *
***************************************************************************
*/
error_reporting(E_ALL);
include(
"dynmethod.php");

class
foo {
    var
$a;
    var
$b;
   
}

?>
<center>
<h1>Testing PHP __call</h1>
</center>
<?php
/* creating an object*/
$f = new foo();
$f->a = "This is a value for a";
$f->b = "This is a value for b";
$f->obj = new foo;
$f->obj->a="another property";

echo
"<h2>Original object</h2>";
echo
"<pre>".print_r($f,true)."</pre>";

echo
"<h2>Adding new methods</h2>";

new
method($f,"bar","print 'hello<br>';");


$code = <<<EOF
print "You can know the method name with magic variable <b>\\\$fnc</b> (\$fnc)<br>";
    print "Also you can know all the parameters with the magic variable <b>\\\$args</b><br>";
    print_r(\$args);
EOF;
new
method($f,array("method1","method2"), $code );

echo
"<h2>Calling methods</h2>";
/* calling */
echo "<h3>MEthod bar</h3>";
$f->bar("param 1","param 2");
echo
"<h3>MEthod method2</h3>";
$f->method2("param 3","param4");

echo
"<h23>New object</h2>";
echo
"<pre>".print_r($f,true)."</pre>";

echo
"<h2>Some warnings</h2>";
print
"Do never do this!: &lt;?php echo get_class(\$f); ?&gt;<br>";
print
get_class($f)."<br>";
print
"Instead of this do this: &lt;?php echo get_class_name(\$f) ?&gt;<br>";
print
get_class_name($f)."<br>";
?>