Nu har jag fått till det som jag vill ha det, ett tydligt felmeddelande när man använder odefinierade egenskaper på en klass (det borde vara inbyggt i ett objektorienterat språk):
Kod:
<?php
class Person
{
private $firstname;
private $lastname;
public function __construct($pFirstname, $pLastname)
{
$this->firstname = $pFirstname;
$this->lastname = $pLastname;
}
public function __set($name, $value)
{
switch ($name)
{
case 'firstname':
$this->firstname = $value;
break;
case 'lastname':
$this->lastname = $value;
break;
default:
# By throwing Exception for undefined property we can enforce strict property definition rules
# we also achive better error messages for improved debugging during development
# this is necessary because of lack of support for strict properties in php 5.3.15
$error = "Error: __set property: " . $name . " not supported by class: " . __CLASS__ ;
throw new Exception($error);
break;
}
}
public function __get($name)
{
switch ($name)
{
case 'firstname':
return $this->firstname;
break;
case 'lastname':
return $this->lastname;
break;
default:
# By throwing Exception for undefined property we can enforce strict property definition rules
# we also achive better error messages for improved debugging during development
# this is necessary because of lack of support for strict properties in php 5.3.15
$error = "Error: __get property: " . $name . " not supported by class: " . __CLASS__ . ".";
throw new Exception($error);
break;
}
return null;
}
/*
* a String representation for all Persons.
*/
public function __toString()
{
return $this->firstname . " " . $this->lastname;
}
}
?>
<?php
$rad = 0;
try
{
echo "\nTest of errormessage wehen faulty property ...\n";
echo "(row) - Firstname Lastname\n";
# create a PHP Array and initialize it with Person objects
$persons = array
(
new Person("Fredrik", "Framberg"),
new Person("Greta", "Gavelstam"),
new Person("Urban", "Urberg"),
new Person("Anna", "Ambtesteg"),
new Person("Henrik", "Hammarberg"),
new Person("Kristina", "Karlestam"),
new Person("Hans", "Hallin"),
new Person("Berit", "Bygdén")
);
# print out the results - calls Person->__toString().
foreach($persons as $person) echo "(" . ++$rad . ") - " . "$person\n";
}
catch (Exception $e)
{
echo 'Caught exception (1): ', $e->getMessage(), "\n";
}
try
{
echo "\nTest of creating new object of class Person ...\n";
$faultyPerson = new Person("Dennis","Harper");
echo "This message will hopefully be written to console.\n";
}
catch (Exception $e)
{
echo 'Caught exception (2): ', $e->getMessage(), "\n";
}
try
{
echo "\nTest of errormessage when faulty __set property ...\n";
echo "(" . ++$rad . ") - " . "$faultyPerson\n";
$faultyPerson->Age = 34;
$faultyPerson->ShoeSize = 45;
echo "Age: " . $faultyPerson->Age . " years.\n";
echo "ShoeSize: " . $faultyPerson->ShoeSize . ".\n";
echo "This message will not be written to console.\n";
}
catch (Exception $e)
{
echo 'Caught exception (3): ', $e->getMessage(), "\n";
}
try
{
echo "\nTest of errormessage when faulty __get property ...\n";
echo "(" . ++$rad . ") - " . "$faultyPerson\n";
echo "ShoeSize: " . $faultyPerson->ShoeSize . ".\n";
echo "Age: " . $faultyPerson->Age . " years.\n";
echo "This message will not be written to console.\n";
}
catch (Exception $e)
{
echo 'Caught exception (4): ', $e->getMessage(), "\n";
}
echo "\nDone testing class: Person.\n---------------------------------------\n\n";
?>