Judging from the responses received so far it sounds like PHP 5 isn't
being used nearly as much as it should.  Nor is OOP.  I'd like to try to
help change that since both are really worth looking into.

Here's a little bit to start off with until I can write a bunch more at
a later date:

PHP 5 has a new function called __autoload()
<http://us2.php.net/__autoload> .  __autoload works with objects and
interfaces, and if you plan things correctly, you will not have to ever
do a require() or require_once() or include() or include_once() ever
again.  Or at least only rarely.  In addition, since you're letting PHP
determine if you need certain code, it only gets included when it's
actually needed.  This means that your pages and code may load and
execute faster.  There's no need to include huge utility files
containing dozens or hundreds of functions that you might only need to
use one or two.

__autoload() is a function you'll need to define in your own code.  It
tells PHP what to do when your code needs an class or interface that it
doesn't already have loaded.  Since you define the function, you can
organize your code in any way that makes sense to you.

The part about not using require() or include() in your code ever again
isn't entirely accurate.  It will be used in your __autoload() function
and it will be needed to be included in your code.  This problem can be
solved using the auto_prepend feature in your PHP.INI file.  Just make
sure your include_path is set up correctly and then add the name of our
autoloader file in to the auto_prepend_file directive.  If you're
running a lot of different sites using a VirtualHost setup in apache,
then you can set your various PHP.INI directives in there.  This lets
you have a different auto_prepend_file for every virtual host.

Now suppose you have set the auto_prepend_file to include the file your
__autoloader() function lives in.  You can now start writing code as
such:

<?php
$myclass=new CrazyClass('arg1', 'arg2');
$myclass->doSomething();
$myclass->doSomethingElse();
?>

When PHP gets to the first line to create an instance of CrazyClass,
it'll check to see if that class is loaded.  Since it isn't, it will
call your __autoload() function to determine how and where to load the
CrazyClass class.

So what could the __autoload() function look like, you might ask?  Well
right now, it could be extremely simple:

function __autoload($classname) {
   require_once($classname . 'php');
}

Pretty simple, huh?  That function will be called any time PHP needs to
load something.  In a future post, I'll talk about how you can actually
use multiple autoloaders.

Anyway, using __autoload() could let you use any sort of criteria you'd
like to figure out what file to load.  For instance if you were
developing a framework, you could name each of your framework classes
with a capital "F".   Then if the class to load started with a capital
"F" then you could have the autoloader load those files from the
framework directory.  The possibilities for making your life easier are
pretty much endless.  At least as far as PHP code goes.

If you've been reading along so far, you saw that __autoload takes a
single parameter:  the name of the class (or interface).  The logical
way to take advantage of this is to organize your code so that each PHP
file contains a class of the same name.  In the above example, my
CrazyClass class should be in CrazyClass.php in the include_path.

If you need to have utility functions, it might be worth it to define
then in an object-oriented way.

Utility.php
<?php
   class Utility {
     public static function boldWrap($string) {
         return '<b>' . $string . '</b>';
     }

     public static function italicWrap($string) {
         return '<i>' . $string . '</i>';
     }

     public static function baconWrap($filet) {
         return '<bacon>' . $filet . '</bacon>';
     }
   }
?>

So now they are object-oriented-ish.  These functions are defined as
both public (which has meaning in PHP 5) and static which means that you
don't have to create an object from the Utility class to use the
functions.  This makes sense since the functions (methods) don't rely on
any particular properties of the Utility class.  In fact, there aren't
even any properties defined.  In non-OOP PHP, these functions would be
defined outside of the class.  You would call them like any normal PHP
function.  Since they are wrapped in a class, they need to be called
slightly differently:

<?php
    echo Utility::baconWrap('steak');
?>

PHP is smart enough to figure out that you want to call the static
baconWrap method in the Utility class in this case.  Since the Utility
class isn't loaded, it will automagically call the __autoload function. 
If you organize your utility classes logically, then you can let
autoloader only load the group of methods that are likely to be needed
together.

It's getting late, so I'll wrap it up.  I'd love to answer questions
about this or hear about how other group members are using __autoload
already.  I'll try to write some more PHP5 stuff later.

Thanks,
David



--- In [email protected], "otkcotsd" <[EMAIL PROTECTED]> wrote:
>
> I've been a member and moderator of this group for quite some time
> now, but haven't been that active in awhile now (other than rejecting
> spam messages and such).  Anyway, I am hoping to start some
> conversations and  work toward increasing the entire group's PHP
> interest level as well as skill level.
>
> To start it off, I'd like to know who is using PHP 5?  Who is
> developing PHP professionally, and who is doing it as a hobby or side
> job?
>
> Would anyone be interested in learning about some advanced topics in
> PHP and OOP based PHP?
>
> Let me know.  I look forward to hopefully getting a ton of emails in
> response and putting some new energy into this group.
>
> Thanks,
> David
>



[Non-text portions of this message have been removed]

Reply via email to