WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
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 Click here to Update Your Picture
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.
Submit your own code examples 
 

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