Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between calling a function by procedural or object oriented style

Tags:

php

mysqli

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.

like image 215
Tyrannogina Avatar asked Feb 23 '16 12:02

Tyrannogina


People also ask

Is procedural better than object-oriented?

Procedural programming languages are not as faster as object-oriented. The object-oriented programming languages are faster and more effective. Procedural uses procedures, modules, procedure calls. Object-oriented uses objects, classes, messages.

What is the difference between procedural and functional programming?

Procedural programming – specifies the steps a program must take to reach a desired state. Functional programming – treats programs as evaluating mathematical functions and avoids state and mutable data.


2 Answers

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;
}
like image 148
deceze Avatar answered Sep 19 '22 03:09

deceze


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;
}
like image 32
fusion3k Avatar answered Sep 22 '22 03:09

fusion3k