First Class problems
Moderator: General Moderators
Re: First Class problems
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?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: First Class problems
Better would be to make error() me a method and change the code you mention to something like $this->errmsg = "error message";psurrena wrote:So, we remove the error function and replace it with echo "error message".
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.psurrena wrote:Why not have the query as $this->query so it can be whatever we want?
(#10850)
Re: First Class problems
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";
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: First Class problems
Well ... no.psurrena wrote:Something like this?
(#10850)
Re: First Class problems
I get "query Empty"
Code: Select all
$newFind = new Work();
$newFind->findAll("SELECT id, title, image FROM work ORDER BY id ASC");- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: First Class problems
That's becaues you have $query=""; setting the string to null. I was thinking more about:
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.
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.
(#10850)
Re: First Class problems
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.
- 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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: First Class problems
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:That work really well. A few questions:
- Any variable that the user has access to has to be declared private/protected/public?
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:- By only having a variable in the construct method, is that the same idea as require()? Since the class is useless with out it?
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:- What do you mean by a gateway or abstracting a table? Isn't a table a table?
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.psurrena wrote:The example you created is really making sense now, I can't thank you enough for all your help.
(#10850)
Re: First Class problems
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
Also, when I run the array through a foreach() loop, I get double results.
I took out the thumbnails array for testing...
Code: Select all
while($row=mysql_fetch_array($result)){
return $row;
}Code: Select all
foreach($row as $value){
echo $value."<br />";
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: First Class problems
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.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?
(#10850)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: First Class problems
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:
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.
Code: Select all
$thumbs = $work->findAll();
foreach($thumbs as $row){
foreach($row as $column => $value){
echo "$column=$value, ";
}
echo "<br />";
}(#10850)
Re: First Class problems
Sorry, I just don't get it, I always think of tables a something referred to.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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: First Class problems
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?
(#10850)
Re: First Class problems
I have what I need BUT the general Gateway class seems really useful at this stage. WorkModel class might be jumping the gun.