auto suggest alternate properties in class
| From: | Daevid Vincent | Date: | Fri, 15 May 2009 04:14:01 +0000 |
| Subject: | auto suggest alternate properties in class | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-292605@lists.php.net to get a copy of this message | ||
Here's a handy little routine I just wrote to suggest properties you're
trying to erroneously set that don't exist in a class. For example:
Attempted to __get() non-existant property/variable 'operator_id' in class
'User'.
checking for operator and suggesting the following:
* id_operator
* operator_name
* operator_code
enjoy.
/**
* Suggests alternative properties should a __get() or __set() fail
*
* @param string $property
* @return string
* @author Daevid Vincent [daevid@daevid.com]
* @date 05/12/09
* @see __get(), __set(), __call()
*/
public function suggest_alternative($property)
{
$parts = explode('_',$property);
foreach($parts as $i => $p) if ($p == '_' || $p == 'id')
unset($parts[$i]);
echo 'checking for <b>'.implode(', ',$parts)."</b> and
suggesting the
following:<br/>\n";
echo "<ul>";
foreach($this as $key => $value)
foreach($parts as $p)
if (stripos($key, $p) !== false) print '<li>'.$key."</li>\n";
echo "</ul>";
}
just put it in your __get() or __set() like so:
public function __get($property)
{
echo "<p><font color='#ff0000'>Attempted to __get() non-existant
property/variable '".$property."' in class
'".$this->get_class_name()."'.</font><p>\n";
$this->suggest_alternative($property);
exit;
}