PHP Classes
Icontem

File: example.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cesar D. Rodas  >  Dynamic Method  >  example.php  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: Dynamic Method
Dynamically add functions to objects
 

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->"This is a value for a";
$f->"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>";
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products