Return to Snippet

Revision: 5703
at March 28, 2008 11:22 by iTony


Initial Code
<?php
class Person {
	private $firstName;
	private $lastName;
	private $age;
	private $country;
	function __call($method, $arguments) {
		$prefix = strtolower(substr($method, 0, 3));
		$property = strtolower(substr($method, 3));
		if (empty($prefix) || empty($property)) {
			return;
		}
		if ($prefix == "get" && isset($this->$property)) {
			return $this->$property;
		}
		if ($prefix == "set") {
			$this->$property = $arguments[0];
		}
	}
}
$personObj = new Person;
$personObj->setFirstName("Pepe");
$personObj->setLastName("Argento");
$personObj->setAge(50);
$personObj->setCountry("Argentina");
echo "Nombre: ".$personObj->getFirstName()." ".$personObj->getLastName()."\n";
echo "Edad: ".$personObj->getAge()."\n";
echo "País: ".$personObj->getCountry()."\n";
?>

Initial URL


Initial Description


Initial Title
magic method call

Initial Tags
php

Initial Language
PHP