[RESOLVED] Class Question for PHP5

liunx

Guest
The quality and quantity of help in this forum has been so great, I can't help pushing my luck with another question.

I've created user defined classes in one Javascript application I developed and for C++ and Java courses I was taking. I knew how, but I didn't understand the why.

My outlook was this ... It's so easy to extract information from a multi-dimensional array by cycling through it, why would you use classes? How the heck would you cycle through all the objects in a class anyway?

I've been reading Beginning PHP5 and MySQL, and I started thinking about classes. This syntax came to mind ...

foreach ($_POST as $key=>$value) {
echo "<p>\$key = $key and \$value = $value</p>";
}

Cycling through a class to extract information--all the employees who make over $50k, for instance--is some variation on this syntax for cycling through an associative array, isn't it?

By the way, adding a Thank You message to the end of a Resolved problem seems a waste of space, so let me thank you in advance for any help or insight you provide.Classes are for so much more than just storing values like an associative array. They include both data and methods (functions) for working with that data. For a basic understanding of what classes and objects are, I'd recommend this on-line article as a good starting point: <!-- m --><a class="postlink" href="http://java.sun.com/docs/books/tutorial/java/concepts/">http://java.sun.com/docs/books/tutorial/java/concepts/</a><!-- m -->

It is part of Sun's Java documentation, but is general enough to describe concepts which will translate to any object-oriented implementation.This wikipedia article (<!-- m --><a class="postlink" href="http://en.wikipedia.org/wiki/Object-oriented_programming">http://en.wikipedia.org/wiki/Object-ori ... rogramming</a><!-- m -->) might also help you get started. I'd also suggest a good introductory OOP book. To put it bluntly with no wish to offend, your current understanding of classes is akin to that of the blind man holding the elephant's tail. Knowing how to mechanically define classes is not the same as knowing how to write and use them.I did visit the web site you suggested, and skimmed the class section from front to back. Except for interfaces, which were further along in the textbooks than my C++ and Java classes got, it was all familiar stuff. I know what classes and objects are and how to create classes and construct and destruct objects. My question is why would I bother?

I'm working mainly with interpreted languages: Javascript, Perl, PHP, and shell scripts. Then I discover PHP5 has added classes. So while I'm reading about PHP5 classes, I ask myself. What good are they for what I do? Should I spend some time mastering PHP5's use of them and incorporating them into my projects?

I have lots of cases where I have to pull subsets of data from a set of data. That's something I can do easily with single or multi-dimensional arrays and functions. Why would I take an OOP approach if I lose this capability? It stands to reason there's a way to do this, but it's something we never covered in any class I took. If I understood how this is done, then OOP becomes practical.A class can have an array as an attribute, which you could manipulate like any other array. That class could have a method which manipulates that array in a particular way, in which case you could call that method instead of directly interfacing with the array (and if you choose to make that array attribute "private", that's the only way you can manipulate it).

Whether you gain anything by using a class in one particular, small-scale example is problematic at best. Object-oriented design comes into play in the large picture, when you are developing large, complicated application and/or code that you want to be reusable and easy to "plug in" to many applications. Then its techniques of defining your application via discreet objects provides both an effective means of organizing your application's design, modularizing it in a "loosely coupled" manner to increase maintainability and ease of debugging, and making it easier to re-use parts of one application in another application.

It took me awhile to understand and embrace object-oriented development, but now I use it in almost all but my most trivial PHP scripts.Say that you create a class called student and then instantiated student objects for each one of your students. Using get and set methods, you could read and/or manipulate the attributes of the individual objects (test grades, seat number, etc.). But what about if you wanted to generate a list of all your male student objects? Or objects that had a >= 94 average? That's what I don't understand how to do, and what is keeping me from diving into OOP. How would I read through my list of student objects to do that? Would the class name be similar to an associate array name, or is it the technique completely different?You'd have an object called ... well, you can't call it class ... and that would contain an array (or some sort of iterator, or some sort of list) of students, and methods for manipulating the whole group. As an array, it would be the usual iterate over the array thing. Consider that "a list of all your male student objects" isn't anything to do with a particular student, so it wouldn't be part of the student object (there would need to be a property of each student that says whether they're male of course).

Say there is an object called $homeEconomics and it contains an array $students of student objects. Each $student has a property "gender" which can contain "male" or "female" (or whatever).

Have a method in the class:
function maleStudents()
{
$maleStudents = array();
foreach($this->students as $student)
if($student->gender=='male')
$maleStudents[]=$student;
return $maleStudents;
}

More fundamentally (and therefore more abstractedly, which therefore means you need to think harder about why you'd need it) some frameworks provide a data access framework (Microsoft's ADO.NET is the first that springs to mind because I'm battling with it right now, but have a look at PHP's PDO framework). They provide generic data access objects and leave you to subclass them with the functionality that you need for the particular application.

As NogDog pointed out, OOP comes into play when you're considering the big picture: any individual specific instance could be done without OOP and there'd be no problems. The problems would come when all these individual specific instances that were implemented separately without OOP have to be put together. (I'm generating a report from stuff in the database. The output might be wanted as XHTML, a spreadsheet, a PDF, as an XML document; the user interface might be Ajax, Java, Flash, XUL, or whatever. Oh, and did I mention that I might get the data in three or four different formats? What I need is to put all the different "user interface", "generate output" and "consume input" routines into separate units and be able to just pass a "ui" object, "generator" object and "consumer" object around in my application and ask them to do their stuff without having to care about what they actually do to achieve those tasks.)Well, if you need to work with a group of students, then you probably need a StudentGroup class of some sort. That class might well contain as one of its attributes an array of Student objects. You could then walk through that array if you need to do some operation on that group of students.So cycling through objects for aggregate information isn't necessarily brain surgery on rocket scientists ...

# Approach 1. If PHP5 lets you create arrays of objects, then the thing to do is to add every new object to the object array as you instantiate it.

# Approach 2 Create a StudentGroup class with an array of Student Objects as a property of the class.

# Approach 3. I have a 6" thick book on C++ and another the same size on Java. In one of them, the author talked about functions (maybe super-methods) that exist outside the class but deal with the objects within the class. I didn't even think about that approach till this discussion expanded. I'll try and track that down and see if PHP5 has a corresponding capability.In C++, "Static members of a class are used to provide class-wide storage of data that is independent of any particular object of the class type, but is accessible by any of them. They record properties of the class as a whole, rather than of individual objects." (Ivor Horton, Beginning C++: The Complete Language) Either object attributes or functions can be static.

In Beginning PHP and MySQL 5: From Novice to Professional, Jason Gilmore explains the use of Static Class Members in PHP5.

So a means for manipulating groups of objects does exist and may be usable for resolving some of the situations I was wondering about.

I'm going to start exploring the possibilities.

Thanks to everyone who offered help.
 
Back
Top