It's better to define the accesor methods according to the property name which that accesor accesses.
- For accesors that set values you can use: setProperty_name
- For accesors that get and return values you can use: getProperty_name
<?php class Test { private $prop; // property without an initial value // accesor to set value for $prop public function setProp($val) { $this->prop = $val; } // accesor to get the value of $prop public function getProp() { return $this->prop; } } ?>Now, in the PHP script we create an object instance of the Test class, then we call the accesor methods to set and get the value of $prop.
<?php // icluding the code of the Test class $obj = new Test(); // calls the setProp() accesor to set a value to "prop" $obj->setProp('Free PHP course'); // calls the getProp() accesor to get the value of "prop" $prop_val = $obj->getProp(); // print $prop_val echo 'The value of the "prop" property is: '. $prop_val; ?>This example will output:
<?php class ArPer { // define private properties private $length, $width; // set constructor public function __construct($length, $width) { // if $length and $width are numeric type, set properties value // else, ends the script if (is_numeric($length) && is_numeric($width)) { $this->length = $length; $this->width = $width; } else { exit('Invalid argument value!'); } } // define getArPer method public function getArPer() { // calculate and output the area and perimeter $area = $this->length * $this->width; $perimeter = 2 * ($this->length + $this->width); echo 'Area: '. $area. ' - Perimeter: '. $perimeter; } } ?>Now, in the script we create an object of this class and call the getArPer() method (the class must be included in the script).
<?php // include ArPer class // create the object $obj = new ArPer(7, 8); // calls the getArPer method $obj->getArPer(); echo '<hr />'; // create another object of the ArPer class, with a string argument $obj2 = new ArPer(7, 'str'); // Invalid argument value! ?>This example will output:
<?php // Test class class Test { private $name; // Constructor public function __construct($name) { // assigns the value of the argument to the $name property $this->name = $name; echo 'Welcome '. $this->name. '<br />'; } // accesor method - change the valure of '$name' public function setName($name) { $this->name = $name; } // Destructor method function __destruct() { echo 'Good by '. $this->name; } } ?>To test the effect of __destruct() method, use the following script (see explanations in script code).
<?php // include Test class // object instance of Test class $obj = new Test('Mar'); // calls setName() to set a new value for 'name' property $obj->setName('Plo'); // Delete the $obj instance unset($obj); ?>PHP detects when the $obj object is deleted (with unset($obj);) and automatically invokes the __destruct() method.
<img src="image.jpg" usemap="#map1"> <map name="map1"> <area shape="rect" coords="9, 120, 56, 149" href="#"> <area shape="rect" coords="100, 200, 156, 249" href="#"> </map>
#id { overflow: auto; }
document.getElementById("id").onmouseover = function(){ document.write("Have Good Life"); }
if(isset($_GET["id"])) { echo $_GET["id"]; }