note 54401 added to language.oop5.patterns
| From: | php-general at lists dot php dot net | Date: | Sun, 03 Jul 2005 06:28:08 +0000 |
| Subject: | note 54401 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91332@lists.php.net to get a copy of this message | ||
Here's an effective strategy for creating singletons.
1.) define an __autoload() function
2.) Define all properties of a class to be static
3.) Make the constructor private.
Now, anywhere in your code, you can simply do as follows:
Class::Function();
The __autoload() function will take care of loading the class.
It's even better when you use the auto_prepend directive to define __autoload(). Now you can
create a php script which looks like this:
<?php
TemplateSystem::Start();
echo 'This is just a test!';
TemplateSystem::Stop();
?>
Now you've effectively got a 2-line solution to all templating issues, the templates will be
available to every script, and the only additional overhead that you'll have for scripts that
don't use the templating system is a 3-line autoload function!
Obviously, there's still a problem with __autoload() if you are attempting to write a
templating system that has to work with someone else's software that also defines __autoload().
The best advice I can give here is to remove one of the __autoload()'s and modify the other to
work with both.
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54401
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54401&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54401&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54401&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54401&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54401&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54401&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54401&report=yes
Search -- http://master.php.net/manage/user-notes.php