Are these sets of code the same?

Hey everyone,

I have recently started coding in php5, my previous experience in using php4 so i want to understand some basic database stuff :slight_smile:

The approach i am taking is to use the MVC structure and OOP as oppose to procedural…

Below is how i used to do a select from a table in php4:

$query = "SELECT * FROM tbl_categories";
$result =  mysql_query ($query);

while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
echo $row['name']..

But as this approach was considered not good practice i did some research and found that it’s better to split code. I now have a Database class where i have several functions all responsible for different things. This is a function i am using:

    public function selectCategory() {
        $sql = 'SELECT * FROM tbl_categories';
        $results = $this->mysqli->query($sql);
        $categoryArray = array();
        while($row = $results->fetch_array(MYSQLI_ASSOC)) {
            $categoryArray[] =  new Category($row);
        return $categoryArray;

And then i call the function and display what i need within a foreach statement.

What i wanted to ask, is this the same as the first approach i have shown? Instead of returning an array could i not make the code simple and on the page i needed this, simply call the function and get the ‘name’ like $row[‘name’]…?

Any suggestions would be appreciated.

Thanks again

The codes are certainly similar, but not the same.

The problem is that there are many queries that may need to be run in different sections of the site, meaning that they are repeated.

It’d much rather have multiple versions of:

$Categories = $CategoryMapper->getAll();
foreach($Categories as $Category){
    echo 'Name: ' . $Category->Name;

Than multiple versions of:

$CategoryQuery = MySQL_Query("SELECT * FROM Categories");
while($Row = MySQL_Fetch_Assoc($CategoryQuery)){
    echo "Name: " . $CategoryQuery['Name'];

So in essence your class is just a pseudo-namespace filed with a different methods (functions) that don’t really belong? No different then what you did in PHP 4 except now you are going though another layer.

Programming in an object notation is more then just putting methods of whatever into some class structure. But to break things down into distinct objects with just the methods that apply to it. For example you would not give a “Car” object a method for injecting fuel into the engine, that would be for the “Engine” object. Case in point, your “selectCategory” method does not belong in the Database object.

I’d say its a stepping stone.

With OOP considerations, it isn’t technically a set of functions because they use a connection unique to that object.

However, with more structure one should separate methods into different mapper classes, passing the main database object to them…

Right, so having all my functions in a database class isn’t right?

I want to benefit from OOP, and have actually built a site using 3 different classes, each relevant to a separate section of the site…

For example: Events.class.php, Galleries.class.php, User.class.php

Is this a better approach instead of having multiple scripts on the same page like i used to do in php4?

I personally feel comfortable with this new approach as i think i have more freedom and control over what i want to get from the database and it makes the pages look a lot neater… :slight_smile:


Getting more and more into OOP, it is not. However, almost everyone whose come from a procedural background does pretty much the same thing when first starting OOP. At least from my own observations…even I did… (:

Hey thanks,

So how would you recommend i can excel my skills and achieve something from it…

I have only started coding in OOP and am coming to terms with objects and classes etc…

But i don’t really understand how they all relate and why it’s needed where it is…

What would you guys recommend so i can understand :eye:

Sorry for all the questions :slight_smile: