Re: First Class problems
Posted: Sun Apr 06, 2008 10:54 am
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?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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?
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";
Well ... no.psurrena wrote:Something like this?
Code: Select all
$newFind = new Work();
$newFind->findAll("SELECT id, title, image FROM work ORDER BY id ASC");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();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.
Code: Select all
while($row=mysql_fetch_array($result)){
return $row;
}Code: Select all
foreach($row as $value){
echo $value."<br />";
}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?
Code: Select all
$thumbs = $work->findAll();
foreach($thumbs as $row){
foreach($row as $column => $value){
echo "$column=$value, ";
}
echo "<br />";
}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.