PHP provides built-in interceptor methods (also called magic methods), which are automatically executed in some special cases, when are accessed undefined methods and properties.
- Magic methods must be defined with "public" attribute.
<?php // AClas class AClas { // define a property which will store the values assigned to undefined properties public $prop = array(); // __get() interceptor method public function __get($name) { // if there is an element '$name' in $prop array, outputs its value // else, display a message if(array_key_exists($name, $this->prop)) echo $this->prop[$name]. '<br />'; else echo 'The <b>'. $name. '</b> not defined.<br />'; } // __set() interceptor method public function __set($name, $value) { // add in $prop array an element with $name as key, and $value. Then outputs a message $this->prop[$name] = $value; echo 'The <b>'. $name. '</b> not exists. It was assigned the value: <i>'. $value. "</i> to the property: <b>prop['$name']</b><br />"; } } // object instance of the AClas class $obj = new AClas(); // access an undefined property $obj->noprop; // It will cause the execution of the __get() method // assign a value to a undefined property $obj->noprop = 'noprop - PHP OOP'; // will cause the execution of the __set() // access again the same undefined property $obj->noprop; // the __get() is automatically invoked // checking directly the element which __set() created it in $prop echo 'Verificare: '. $obj->prop['noprop']; ?>The $prop property is defined as array to store the values of undefined properties, which are intercepted by the __set() method.
public function __call($method, $arg_array)$method will contain the name of the undefined method.
<?php // AClas class class AClas { // __call() method public function __call($name, $args) { // displays a string withe the undeclared method ant its arguments echo 'There is no <b>'. $name. '</b> method. Arguments: <i>'. implode(', ', $args). '</i>'; } } // object instance of the AClas class $obj = new AClas(); // calling an undefined method $obj->site('coursesweb', 'tutorials'); // will automatically invoke the __call() method ?>This code will output:
<?php // AClas class class AClas { private $msg; // Proprietate // Constructor public function __construct($msg) { // sets the value of "msg" property with the $msg attribute $this->msg = $msg; } // __toString() method public function __toString() { return $this->msg; // return the $msg property } } // object instance of the AClas class $obj = new AClas('Have a good life'); // prints the object instance as a normal variable string type echo $obj; // will cause the auto-calling of __toString() ?>echo $obj; outputs what the __toString() returns.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net