... | ... | @@ -9,7 +9,7 @@ you are |
|
|
|
|
|
today we will create a site homepage which lists a summary of all blog entries, and allows us to click on an individual entry to see the full content.
|
|
|
|
|
|
# step-3
|
|
|
# step 3
|
|
|
|
|
|
## index.php
|
|
|
|
... | ... | @@ -80,7 +80,7 @@ href="index.php?page=blog&id=$entry->entry_id" |
|
|
|
|
|
once you've finished the view file, include it in the blog controller, and check this gives you a view of all of the entries.
|
|
|
|
|
|
# step-4
|
|
|
# step 4
|
|
|
|
|
|
## showing individual blog entries
|
|
|
|
... | ... | @@ -145,3 +145,44 @@ i.e. inside the $entryClicked loop, you'll have |
|
|
|
|
|
check this works ok.
|
|
|
|
|
|
# step 5
|
|
|
|
|
|
## refectoring
|
|
|
|
|
|
one of the motivations of using an object oriented approach is to modularise our code and avoid duplication.
|
|
|
|
|
|
however, if you look at the model we have created i.e. the Blog_Entry_Table class, you can see that there is a lot of duplication between the getAllEntries method and the getEntry method.
|
|
|
they contain very similar lines of code for preparing and executing the sql statement.
|
|
|
|
|
|
so we're going to refactor by breaking that repeated code out in to a method of it's own called makeStatement
|
|
|
|
|
|
## default values
|
|
|
|
|
|
however, there is a difference between the two versions. one uses
|
|
|
```
|
|
|
$statement->execute();
|
|
|
```
|
|
|
and the other uses
|
|
|
```
|
|
|
$statement->execute( $data );
|
|
|
```
|
|
|
|
|
|
how can you wrap these in a single method that can deal with cases with or without $data?
|
|
|
the answer is that you can use a default value when passing parameters to the function.
|
|
|
|
|
|
we can declare the method in the following way:
|
|
|
```
|
|
|
public function makeStatement ( $sql, $data = NULL )
|
|
|
```
|
|
|
the default value will be used if nothing else is passed when the method is called.
|
|
|
|
|
|
write your makeStatement function this way and rewrite the other two methods. to call the new method, the other methods will need to make use of the $this keyword.
|
|
|
|
|
|
## private methods
|
|
|
|
|
|
the makeStatement method is only called internally in the class and is not meant to be called from outside. it is sensible to to use the 'private' access modifier. so change your declaration of the method to
|
|
|
```
|
|
|
private function makeStatement ( $sql, $data = NULL )
|
|
|
```
|
|
|
|
|
|
|