BlazeMike Posted May 27, 2014 Share Posted May 27, 2014 Hi everyone,Until now i didnt use classes and functions for sql related actions and now i would like to start.I started out with this :class.php--------------------------------------------------- class db { public function connect($database,$user,$password) { $db = mysql_connect("localhost",$user,$password) or die (mysql_error()); mysql_select_db($database); } static function fetch() { function getresults($query) { $resultArray = array(); $fetch = mysql_query($query) or die (mysql_error()); while ($row = mysql_fetch_assoc($fetch)){ $resultArray[] = $row; } return $resultArray; mysql_free_result($fetch); } } } and index.php---------------------------- <?php require('class.php'); $connection = new db(); $connection->connect("testdb","root","root"); $connection->fetch()->getresults("SELECT * FROM users");foreach (getresults() as $user) { echo "<div>{$user['user_first_name']}</div>";} BUt it doesnt work. Actually the only way it works is : $connection::fetch();foreach (getresults("SELECT * FROM users") as $user) { echo "<div>{$user['user_first_name']}</div>";} Now my ultimate goal is :- I want to access db and get results anyware in my script to use :$connection->fetch("SQL QUERY");and then foreach to echo the results.- I want to put more functions in fetch. For example if i want to fetch number of results to use$connection->fetch()->results();Im sorry if i confused you!Basically i want to make everything tidy but im not familiar with the logic. I know basic class/function use.Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/ Share on other sites More sharing options...
trq Posted May 27, 2014 Share Posted May 27, 2014 You seem to have a function within a function. That will not work as expected. If you trying to do method chaining, your need to have your methods that wouldn't normally return any useful result return an instance of the current object. Quote Link to comment https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/#findComment-1481012 Share on other sites More sharing options...
Jacques1 Posted May 27, 2014 Share Posted May 27, 2014 Hi, this class has already been written for you: PDO. Note that the old mysql_* functions are long obsolete and will be removed in one of the next PHP releases. Didn't you see the big red warnings in the PHP manual? When you switch to PHP 5.5, you'll also get tons of deprecation warnings for your script. So using this dying extension as the basis for your new database class really isn't a good idea. Quote Link to comment https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/#findComment-1481020 Share on other sites More sharing options...
BlazeMike Posted May 27, 2014 Author Share Posted May 27, 2014 (edited) Thanks for your replies. Hmm ok i will study PDO, seems to be a must! by the way PDO is just for databases, answeing my initial post will help me understand classes/functions in general! I cannot use class->fuction->function? im ive seen scripts using that :| Edited May 27, 2014 by BlazeMike Quote Link to comment https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/#findComment-1481025 Share on other sites More sharing options...
BlazeMike Posted May 27, 2014 Author Share Posted May 27, 2014 by the way i have a question. I have developed some web applications in the past based on old mysql functioning. If i was asked to make changes or aditions to that code should i migrate ALL the code to PDO or should i just make the requested changes in Mysql? Quote Link to comment https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/#findComment-1481032 Share on other sites More sharing options...
Ch0cu3r Posted May 27, 2014 Share Posted May 27, 2014 (edited) I cannot use class->fuction->function? It is possible to do that, it is called method chaining. Here is an example of how this is done. by the way i have a question. I have developed some web applications in the past based on old mysql functioning. If i was asked to make changes or aditions to that code should i migrate ALL the code to PDO or should i just make the requested changes in Mysql? If you want your current code to compatible with future versions of PHP then now would be a good time to do so. Edited May 27, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/#findComment-1481033 Share on other sites More sharing options...
Jacques1 Posted May 27, 2014 Share Posted May 27, 2014 I cannot use class->fuction->function? You can, but you need to understand what this expression actually means. This has nothing to do with nested methods. The inner method simply returns an object, and then you call a method of that returned object: <?php // the short form: $base_object->some_method()->some_other_method(); // what this actually does: $returned_object = $base_object->some_method(); $returned_object->some_other_method(); Of course you can continue the chaining if the outer method again returns an object etc. Quote Link to comment https://forums.phpfreaks.com/topic/288801-question-on-classes-functions/#findComment-1481036 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.