|
|
|
| Title : |
Object() = Custom __autoload + Singleton. "automagically" instantiates a class and always retuns the same instance of the same class. It's pretty useful when you want to have persistence in objects. |
| Categories : |
PHP, PHP Classes, Algorithms |
 Alix Axel |
| Date : |
Apr 28th 2009 |
| Grade : |
1 of 5 (graded 1 times) |
| Viewed : |
1783 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Alix Axel |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
This function also sets you free from include/require(_once)'s.
|
<?php
function Object($object)
{
static $result = array();
if (class_exists($object, false) === true)
{
if (array_key_exists($object, $result) === false)
{
$result[$object] = new $object();
}
return $result[$object];
}
else if (is_file('./' . $object . '.php') === true)
{
$class = basename($object);
if (array_key_exists($class, $result) === false)
{
if (class_exists($class, false) === false)
{
require('./' . $object . '.php');
}
$result[$class] = new $class();
}
return $result[$class];
}
return false;
}
?> | |
Usage:
| <?php
Object('path/to/your/class')->yourClassMethod();
// now that your class is loaded we don't need to specify it's path
Object('class')->yourClassMethod();
// Example:
Object('libraries/DB')->Connect('name', 'user', 'pass', 'host');
Object('DB')->Query('SELECT * FROM users;');
?> | | |
|
| Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags. Categories : PHP, PHP Classes, Algorithms, Security | | | A class to put get and post variables in hidden form
elements. Works on scalars, normal arrays, associative
arrays. Categories : Algorithms, Variables, Arrays, PHP, PHP Classes | | | Timer - a class that uses microtime() to provide easy calculation of elapsed times Categories : Algorithms, PHP, PHP Classes | | | Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers. Categories : PHP, PHP Classes, Credit Cards, Ecommerce, Algorithms | | | Calculate the great circle distance between two latitude/longitudes
Categories : Algorithms, PHP | | | XML Menu Categories : PHP, PHP Classes, Navigation, XML, XSL | | | Browser Detecor Class Categories : PHP Classes, PHP, HTML | | | Building a basic error handler with custom error types Categories : PHP, PHP Classes, Errors and Logging | | | Blueshoes PHP Application Framework Categories : PHP, Frameworks, PHP Classes | | | PostGreSQL and MySQL 2 in 1 db Manager Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL | | | An updated OOP - Inheritance Categories : PHP, PHP Classes, Object Oriented | | | Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip Categories : Email, Network, PHP, PHP Classes | | | ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and
Checks / ACH Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP | | | A very basic and fast XML parser Categories : PHP, PHP Classes, XML | | | php for odbc /* connect to access from odbc */ Categories : PHP Classes, ODBC, MS Access, PHP | |
|
|