DB_DataObject::factory()

DB_DataObject::factory() – Autoload and instantate class based on table name.

Synopsis

mixed DB_DataObject::factory ( string $table )

Description

This is the recommended way to autoload a class, and instantate it. The class is loaded based on the configuration settings (class_location and class_prefix) for table to class naming.

Parameter

  • string $table - the table you want to load ([From Version 1.7.2] if blank, and called on an an instance of a dataobject, it will create a new instance of that object)

Return value

object mixed - DB_DataObject_Error or the object

Throws

Possible PEAR_Error values
Error code Error message Meaning Solution
DB_DATAOBJECT_ERROR_NOCLASS "could not autoload $class"    

Note

This method can be called statically or dynamically.

Example

Simple fetch of data based on Primary Key

<?php
// set up our options
$opts = &PEAR::getStaticProperty('DB_DataObject','options');
$opts = array(
    
'class_location'  => '/home/me/Projects/myapplication/DataObjects',
    
'class_prefix'    => 'DataObjects_'
);


// loads the file: /home/me/Projects/myapplication/DataObjects/Person.php
// and checks that the class DataObjects_Person exists, and returns an
// instance of it.

$person DB_DataObject::factory('person');


if (
$person->get(12)) {
  
print_r($person);
} else {
  echo 
"NO person 12 exists";
}



// it can also be used in a dynamically
class DataObjects_MyTable {
    
    function 
anExample() {
        
$person $this->factory('person');
        
        
        
        
// supported in version 1.7.2
        
$another_mytable $this->factory();
        
        
$another_person $person->factory();
        
    }
}
?>
creating the base Classes and Database schema (Previous) Simple Get (Select) request (Next)
Last updated: Sat, 20 Dec 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

Note by: powtac@gmx.de
You can use staticGet() for primary key querys without factory!