Tyrannogyna
Tyrannogyna

Reputation: 621

Difference between calling a function by procedural or object oriented style

I need to use a simple function: mysqli_num_rows(), but I wanted more general knowledge answer.

Are there any differences between calling this function through object oriented style $mysqli_result->num_rows; or procedural style mysqli_num_rows( mysqli_result $result );?

I understand that the OO, as explained here, is accessing a variable, and the procedural call works as a function, but both return the same thing.

The code in my company is procedural and we are slowly migrating to OOP, but it's mostly chaos, so there aren't any internal guidelines that I could (or would like to) follow.

Upvotes: 6

Views: 1660

Answers (3)

fusion3k
fusion3k

Reputation: 11689

The main difference is only about your preferred style.

In most cases (probably all), the function is a “shortcut” to the oo way.

This two call are equivalents:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

because — substantially — the definition of mysqli_connect is this:

function mysqli_connect( $host, $user, $pass, $db )
{
    $conn = new mysqli( $host, $user, $pass, $db );
    return $conn;
}

Edit: the longhand

See — as example — the 3rd part class simple_html_dom. The object oriented way to load a file is:

$dom = new simple_html_dom();
$data = file_get_contents( $url ) or die( 'Error retrieving URL' );
$dom->load( $contents ) or die( 'Error loading HTML' );

The above three line can be condensed with the procedural call:

$dom = file_get_html( $url ) or die( 'Error loading HTML' );

because the internal code of file_get_html is the following (simplified by me):

function file_get_html( $url )
{
    $dom = new simple_html_dom();
    $contents = file_get_contents( $url );
    if( empty($contents) || strlen($contents) > MAX_FILE_SIZE )
    {
        return false;
    }
    $dom->load( $contents );
    return $dom;
}

Upvotes: 1

BugHunterUK
BugHunterUK

Reputation: 8958

The difference is one is a function, the other is a method. Use whichever you fancy.

But, if your company is slowly migrating to an OO approach, it would be best to stick to using objects. For one, doing so will allow you to take full advantage of dependency injection.

index.php

<?php

    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

    /*
     * Dependency Injection
     */
    $userService = new UserService($db);
    $users = $userService->getUsers();

UserService.php

<?php

class UserService
{
    private $db;

    public function __construct($db)
    {
        $this->db = $db;
    }

    public function getUsers()
    {
        if ($result = $this->db->query("SELECT username, city FROM users"))
            return $result->num_rows;
        else
            return false;
    }
}

This allows you to reuse objects. You're being DRY (Don't Repeat Yourself).

There's no disadvantage to using a mysqli or PDO object even in procedural code. You get the benefit of cleaner code.

Upvotes: 0

deceze
deceze

Reputation: 522372

No, there is no difference. The procedural way is pretty much just a wrapper around the OO API. Historically it was included to allow developers for whom OO was a complete mystery to transition to a better alternative from the mysql API.

For all intents and purposes mysqli_num_rows does this:

function mysqli_num_rows(mysqli_result $result) {
    return $result->num_rows;
}

Upvotes: 6

Related Questions