Page 2 of 3

Re: First Class problems

Posted: Sun Apr 06, 2008 10:54 am
by psurrena
So, we remove the error function and replace it with echo "error message". Why not have the query as $this->query so it can be whatever we want?

Re: First Class problems

Posted: Sun Apr 06, 2008 5:18 pm
by Christopher
psurrena wrote:So, we remove the error function and replace it with echo "error message".
Better would be to make error() me a method and change the code you mention to something like $this->errmsg = "error message";
psurrena wrote:Why not have the query as $this->query so it can be whatever we want?
That sounds like a good idea. I am not sure whether you are talking about the SQL or the database connection. Whichever, let's give your idea a try -- post a little code to show your idea. :)

Re: First Class problems

Posted: Sun Apr 06, 2008 5:55 pm
by psurrena
Something like this?

Code: Select all

 
class Work {
 
   public function __construct($query) {
        $query="";
        $result=mysql_query($query) or die (mysql_error());
 
        $thumbnails = array();
        if(mysql_num_rows($result) < 1){
            $this->errorMsg="There was no content found.";
        }else{
            while($row=mysql_fetch_array($result)){
                $thumbnails[] = $row;
        }
        return $thumbnails;
    }
}
 
$newFind = new Work();
$newFind->query="SELECT id, title, image FROM work ORDER BY id ASC";
 

Re: First Class problems

Posted: Sun Apr 06, 2008 7:09 pm
by Christopher
psurrena wrote:Something like this?
Well ... no. ;) The problem is that the __construct() method cannot return a value, because in the context it is used the object is returned ($work = new Work(); // calls __construct()). So try passing the SQL to the constructor and keeping the findAll() function like I showed above (hint: you will need a property).

Re: First Class problems

Posted: Mon Apr 07, 2008 9:58 am
by psurrena
I get "query Empty"

Code: Select all

$newFind = new Work();
$newFind->findAll("SELECT id, title, image FROM work ORDER BY id ASC");

Re: First Class problems

Posted: Mon Apr 07, 2008 11:23 am
by Christopher
That's becaues you have $query=""; setting the string to null. I was thinking more about:

Code: Select all

class Work {
   protected $query;
   protected $errorMsg = '';
 
   public function __construct($query) {
        $this->query = $query;
   }
 
   public function findAll() {
        $result=mysql_query($this->query) or die (mysql_error());
 
        $thumbnails = array();
        if(mysql_num_rows($result) < 1){
            $this->errorMsg="There was no content found.";
        }else{
            while($row=mysql_fetch_array($result)){
                $thumbnails[] = $row;
        }
        return $thumbnails;
    }
}
 
$newFind = new Work("SELECT id, title, image FROM work ORDER BY id ASC");
$rows = $newFind->findAll();

Now it is initialized when created and ready to use, so you could pass it to some other function to use for example. But there are still some problems with it.

Also, because you give it the full query should not be named Work (if could be any table). So you need to decide whether you want to make it generic database gateway class or specific class that abstracts the work table.

Re: First Class problems

Posted: Mon Apr 07, 2008 12:47 pm
by psurrena
That work really well. A few questions:
- Any variable that the user has access to has to be declared private/protected/public?
- By only having a variable in the construct method, is that the same idea as require()? Since the class is useless with out it?
- What do you mean by a gateway or abstracting a table? Isn't a table a table?

The example you created is really making sense now, I can't thank you enough for all your help.

Re: First Class problems

Posted: Mon Apr 07, 2008 1:18 pm
by Christopher
psurrena wrote:That work really well. A few questions:
- Any variable that the user has access to has to be declared private/protected/public?
private means only this class can access -- not even classes that inherit this class can access it. protected means this class and inheriting classes can acces it -- but still all internally. public means that it can be accessed from outside the object (e.g. $work->query). I would recommend almost always using protected. There are rare exceptions where private makes sense, but they will be obvious when they occur. public is mainly used for Value Objects which means using an object as a data structure, or for things like fluent interfaces. But start with protected are you will go far.
psurrena wrote:- By only having a variable in the construct method, is that the same idea as require()? Since the class is useless with out it?
I guess that is one way of looking at it. The idea with the constructor is that the object is ready to use once created, so anything it is dependent on externally for it to work should usually be passed there.
psurrena wrote:- What do you mean by a gateway or abstracting a table? Isn't a table a table?
Well there are two directions we can go now. One is to create a standard interface that you will use to get records from tables -- that's a Gateway. The other is to abstract your "work" table to add an logic related to it to the class. For example, you might want to be able to iterate and have getImage() and getCaption() methods. That way you are abstracting the flat table into what is usually referred to as a Model. Maybe you want to try both? They are both useful in different circumstances.
psurrena wrote:The example you created is really making sense now, I can't thank you enough for all your help.
My pleasure. A problem with OO is that it is a little counter-intuitive, so you really need to solve one of your own problems to start to see it.

Re: First Class problems

Posted: Mon Apr 07, 2008 1:34 pm
by psurrena
In terms of the table, does gateway table describe what is in place now? The Model seems really interesting but I don't want to get ahead of myself...The getImage() / getCaption() would be useful but what is "abstract" about that? Would we have a separate tables for the images and captions?

Re: First Class problems

Posted: Mon Apr 07, 2008 1:56 pm
by psurrena
Also, when I run the array through a foreach() loop, I get double results.

Code: Select all

while($row=mysql_fetch_array($result)){
   return $row;
}

Code: Select all

foreach($row as $value){
    echo $value."<br />";
}
I took out the thumbnails array for testing...

Re: First Class problems

Posted: Mon Apr 07, 2008 2:49 pm
by Christopher
psurrena wrote:In terms of the table, does gateway table describe what is in place now? The Model seems really interesting but I don't want to get ahead of myself...The getImage() / getCaption() would be useful but what is "abstract" about that? Would we have a separate tables for the images and captions?
What we have above is neither -- it could go either way at this point. By 'abstract' it means that your code does not think of 'work' as a table any more. It thinks of it as an object and internally the object makes everything just work.

Re: First Class problems

Posted: Mon Apr 07, 2008 2:53 pm
by Christopher
If you look at my code above you will see that it is returning all rows as an array of arrays, so it would be:

Code: Select all

$thumbs = $work->findAll();
foreach($thumbs as $row){
    foreach($row as $column => $value){
        echo "$column=$value, ";
    }
    echo "<br />";
}
Notice how in the code above there is nothing really about databases. We don't know where findAll() gets it data -- and this code should not care. In fact, we have now made it so it cannot care. That is what it means to create a Model.

Re: First Class problems

Posted: Tue Apr 08, 2008 9:30 am
by psurrena
What we have above is neither -- it could go either way at this point. By 'abstract' it means that your code does not think of 'work' as a table any more. It thinks of it as an object and internally the object makes everything just work.
Sorry, I just don't get it, I always think of tables a something referred to.

Re: First Class problems

Posted: Tue Apr 08, 2008 11:03 am
by Christopher
So do you want to create a WorkModel class that is specific to this task? Or a general Gateway class that makes accessing table data easier? Or do you have what you need?

Re: First Class problems

Posted: Tue Apr 08, 2008 10:44 pm
by psurrena
I have what I need BUT the general Gateway class seems really useful at this stage. WorkModel class might be jumping the gun.