Jump to content

OOP Concepts


Recommended Posts

Hi,

 

Please consider the following snippet:

 

$mysqli = new mysqli('host', 'user_name', 'password', db_name''); //line 1

$result = $mysqli->query($query); //line 2

$r = $result->num_rows; //line 3

 

I am pretty comfortable with the line 1 and 2; line 1 creates an object called '$mysqli' and because mysqli class as built-in class takes 4 arguments the line 1 is perfectly fine. And line 2, it accesses to the method, which is already defined in the mysqli class, called query which takes one argument using access operator(->).

 

But I have no idea why the access operator(->) is used on line 3. I think the variable $result is not an object but just a variable in terms of OOP(because it didn't create any object unlike $mysqli). And I found that "$mysqli->query($query)->num_rows;" is also possible. Could you explain what is going on with the line 3 please?!

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/
Share on other sites

Hi,

 

Please consider the following snippet:

 

$mysqli = new mysqli('host', 'user_name', 'password', db_name''); //line 1

$result = $mysqli->query($query); //line 2

$r = $result->num_rows; //line 3

 

I am pretty comfortable with the line 1 and 2; line 1 creates an object called '$mysqli' and because mysqli class as built-in class takes 4 arguments the line 1 is perfectly fine. And line 2, it accesses to the method, which is already defined in the mysqli class, called query which takes one argument using access operator(->).

 

But I have no idea why the access operator(->) is used on line 3. I think the variable $result is not an object but just a variable in terms of OOP(because it didn't create any object unlike $mysqli). And I found that "$mysqli->query($query)->num_rows;" is also possible. Could you explain what is going on with the line 3 please?!

 

Thanks

 

Does $mysqli->query($query) not act as a constructor? If it doesn't then you are right. It's not truly OOP and they are using it the wrong way.

 

Edit: When I say act as a constructor I mean does it call a constructor of its own and return a class object

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/#findComment-1055835
Share on other sites

Does $mysqli->query($query) not act as a constructor? If it doesn't then you are right. It's not truly OOP and they are using it the wrong way.

 

Edit: When I say act as a constructor I mean does it call a constructor of its own and return a class object

 

The method query() returns a MySQLi_STMT object, num_rows is a property that contains the number of rows by the last executed query.

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/#findComment-1055840
Share on other sites

Does $mysqli->query($query) not act as a constructor? If it doesn't then you are right. It's not truly OOP and they are using it the wrong way.

 

Edit: When I say act as a constructor I mean does it call a constructor of its own and return a class object

 

The method query() returns a MySQLi_STMT object, num_rows is a property that contains the number of rows by the last executed query.

 

From what I'm reading on the php docs the query method returns a result object

 

Return Values

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE.

 

http://php.net/manual/en/mysqli.query.php  ;)

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/#findComment-1055854
Share on other sites

From what I'm reading on the php docs the query method returns a result object

 

I am well aware of what the query() method returns, thank you. And I am also more then familiar with the PHP.net manual, thank you. I just copy-pasted the wrong class name earlier.

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/#findComment-1055859
Share on other sites

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE.

 

If does the result object mean mysqli_result object(or class)? If then, it makes sense because up to "$mysqli->query($query)", it is the mysqli_result object(  :confused: OR class???  :confused: ). And the class mysqli_result does have a property called "num_rows". So "$mysqli->query($query)->num_rows" sounds perfect.

 

One final question, though,  is when it says "blah blah blah will return a result object" what excatly would it be? I am a bit confused.

 

Thanks

 

 

Edit: What's my point is that if it returns a result object how are they formed? It is getting kind of hard-to-explain question. Sorry about that.

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/#findComment-1055873
Share on other sites

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE.

 

If does the result object mean mysqli_result object(or class)? If then, it makes sense because up to "$mysqli->query($query)", it is the mysqli_result object(  :confused: OR class???  :confused: ). And the class mysqli_result does have a property called "num_rows". So "$mysqli->query($query)->num_rows" sounds perfect.

 

One final question, though,  is when it says "blah blah blah will return a result object" what excatly would it be? I am a bit confused.

 

Thanks

 

What do you mean what exactly would it be? It's saying that the query method's return type is a mysqli_result object. mysqli_result is a class that contains the public method num_rows. There for what you are doing above with $mysqli->query($query) is creating a result object. Once that result object is created you are calling its public method with ->num_rows

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/#findComment-1055884
Share on other sites

I think he has problems understanding the difference between a class and an object. The most common explanation they give, and in most cases gets the point across is that you can compare it to a blueprint of a house and a house itself respectively a class and an object.

 

In simple terms: an object is created using the class as a blueprint.

 

 

Link to comment
https://forums.phpfreaks.com/topic/201261-oop-concepts/#findComment-1055898
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.