The difference between calling a function in a procedural or object-oriented style

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

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

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

The code in my company is procedural, and we are slowly moving to OOP, but this is mostly chaos, so there are no internal recommendations that I could (or would like) to implement.

+5
source share
3 answers

No, there is no difference. The procedural way is just a wrapper around the OO API. Historically, this was included to allow developers for whom OO was a complete secret to migrate to a better alternative from mysql API.

For all purposes and tasks, mysqli_num_rows does the following:

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

The main difference is only your preferred style.

In most cases (possibly all), the function is a shortcut to the oo path.

These two calls are equivalents:

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

because - essentially - the definition of mysqli_connect is as follows:

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

Edit: long

See as an example the class of the third part simple_html_dom . Object oriented file upload method:

 $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 lines can be compressed with a procedural call:

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

because the internal code of file_get_html is as follows (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; } 
+1
source

The difference is that one function and the other is a method. Use what you like.

But, if your company is slowly moving to the OO approach, it is best to use objects. Firstly, it will allow you to make full use 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 are dry (do not repeat yourself).

There is no shortage of using a mysqli or PDO object even in procedural code. You benefit from cleaner code.

0
source

Source: https://habr.com/ru/post/1243658/


All Articles