On Tue, 28 Jan 2003 16:20:20 +0100, Maxim Maletsky wrote:

> 

I use PHP classes extensively and very often use classes
within other classes.

First off, I would make sure to take advantage of inheritence
as much as possible. I have heirarchies up to 4 deep for
some of my classes depending on how much specialization
I need while at the same time maintaining only one set
of code for functionality common to all subclasses.

As for using classes within classes, I don't include
the classes within a class definition nor within 
a method def. I include the class def in the class
file but outside the actual class {} statement.

When I instantiate the class, if I will need the object
ref to pass to other classes or to other methods within
the calling class, I assign the initial object
instantiation to a class variable. That way any class
methods can have ready access to that class via the 
ojbect reference stored in the class variable. 

Only when I know for sure that the use of a class will
be for one purpose do I include and instantiate within
a method. 

as always, YMMV depending on the structure of your project.

NOTE: There is a set of new PHP functions that allow
a sort of dynamic multiple inheritence. You can look
here for info: 

http://www.php.net/manual/en/ref.objaggregation.php

Jeff



> "@ Nilaab" <[EMAIL PROTECTED]> wrote... :
> 
>> Hello everyone,
>> 
>> I want to be able to use objects to create my future pages. My goal is
>> to use methods of classes to make the original front-line script easier
>> to read, while all the processing is done with a simple call to the
>> different classes from a single class. Please read futher, as I'll get
>> to a point and to my question...
>> 
>> I have many classes that do different tasks, like formValidator.class,
>> stringManipulator.class, db.class, fileManipulation.class,
>> template.class, etc (these are self-explanitory as their names suggest).
>> Then, I might have a class called category.class that adds, deletes,
>> edits, moves, and renames categories within the filesystem and database.
>> But I would have a front-line script called category.php that would call
>> the necessary methods of category.class at certain points, depending on
>> the task being done on a specific step.
>> 
>> In other words I want category.class to call the other classes and do
>> something with them, then in turn I want category.php to call objects in
>> category.class for a specific task, such as:
>> 
>> <?php
>> 
>> // category.php
>> 
>> include ("category.class");
>> $cat = new category ();
>> $cat->addCategory($new_cat_name);
>> // or
>> $cat->editCategory($cat_name);
>> // or
>> $cat->deleteCategory($cat_name);
>> // or
>> $cat->moveCategory($cat_name);
>> // or
>> $cat->renameCategory($cat_name);
>> 
>> ?>
>> 
>> 
>> My question is:
>> 
>> How can I call a class within another class and do something with it?
>> Right now I'm doing it the most convenient way I know, which is
>> including other classes using the include() function within the methods
>> of the category.class. There is no multiple-inheritance allowed in PHP,
>> so I can only use inheritance on one class.
> 
> Including new classes within the existing classes is not such a bad idea
> as it ensures you to have only the necessary classes called.
> 
>> I am also extremely skeptical about creating too many classes at a time
>> in one script. Do the above examples degrade performance speed of the
>> script when I call too many classes? Also, isn't there a way to use
>> sessions to save created classes and then use them again for other
>> scripts without the need to make a new instance of the same class again
>> and again?
> 
> yes, you can serialize/unserialize classes into the sessions. This makes
> it a little more complicated, but can be helpful sometimes.
> 
>> I am really looking for a better way to organize my code while still
>> being able to use these classes whenever I need them and at the same
>> time keeping the category.php file clean and easy to read. Is there a
>> tutorial on how to organize code? I'm not looking for html template
>> tutorials. Just how to get around inheritance limits while still keeping
>> performance and clean-code in mind.
> 
> You know what I have once done? I created a file with functions that
> return you the object pointers. It would create (declare) the class
> whenever it was not declared before or just return the pointer from a
> global variable if it was declared before. That way, you only load a few
> functions, and whenever you need a class you assign a variable to the
> function's return to have the class. This limits you script to only
> classes you use and no includes within the script itself. A kind of silly
> method, but can be easy to work with.
> 
> 
> Also, check out the new Zend 2 engine, it has tons of improvements with
> classes for PHP5. Currently the code is in CVS (checkout php5 module).
> Changes are listed here:
> 
> http://cvs.php.net/co.php/ZendEngine2/ZEND_CHANGES
> 
> 
> --
> Maxim Maletsky
> [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to