->set*() and ->get*()

->set*() and ->get*() – Automatic Setters and Getters using overload

Synopsis

true | string $DB_DataObject->set* ( mixed $value )

mixed $DB_DataObject->get* ( )

Описание

From version PHP 4.3.2RC2 onwards, DB_DataObject is automatically overloaded, providing access to all variables using $object->set{ColumnName}() and $object->set{ColumnName}($value) even if you have not defined the method.

It is assumed that set methods return strings as errors or TRUE, so that it can interact with setFrom and return array's of error strings.

The get Methods are used by toArray(), if defined they can be used to alter the appearance of columns ,like making dates human readable

The logic is very simple, if you call $object->setXXX() and it is not defined, it will just set the value, if you define a method setXXXX, that will be called instead of the default handler, same applies to getXXX().

Due to the naming conflict possiblity of a column named from, the associated method for column 'from' is set_from, rather than setFrom()

Parameter

  • mixed $value - on setters only (the value to assign to the column), on getters you may like to implement date formating or sprintf formating as the argument.

Return value

mixed - setters will return TRUE from the default method, in your implementations of setters. It is expected that setXXX($value) will return a string (the error) if it is invalid or TRUE on success. getXXX may return the value or a formated value, remember though it affects $object->toArray().

Замечание

This function can not be called statically.

Warning: This is experimental, its behavour may change slightly in the future.

Пример

Simple find and fetch of data based on Object Vars

<?php
class DataObjects_Person extends DB_DataObject {

    var 
$id;
    var 
$name;
    var 
$date_of_birth;
    
    
// you can define this method after you implement a call to it!
    // as it is automatically implemented to return $this->date_of_birth by 
    // the overload __call() method.
    
    
function getDate_of_Birth() {
        return 
date('d M Y'strtotime($this->date_of_birth));
    }
}
       
       
$person = new DataObjects_Person;

$person->get(12);

// now lets use the getters and setters even though some are not defined.
echo $person->getName() . ' was born on  ' $person->getDate_of_Birth();
?>

Resulting Output

 
       
       Fred Blogs was born on 1 April 1970
    Поддержать сайт на родительском проекте КГБ