xnowandtheworldx Posted July 19, 2009 Share Posted July 19, 2009 Alright, since the depository is read only I'll just post this here . I had one hell of a time trying to figure out how I might be able to display variables via method chaining in PHP5 and I couldn't really find anything anywhere about it except for chaining methods, so...I decided to post this just in case someone else might be wondering . What am I talking about exactly? With the MySQLi class you can echo rows like so... $db = new MySQLi('localhost','username','password','database'); $q = 'SELECT * FROM table'; if($result = $db->query($q)) { while($row = $db->fetch_object($result)) { echo $row->mysql_column_name //this is what I'm talking about.... } } This is how I was able to do it... class ClassName { private $array; //set up a variable to store our array /* * You can set your own array or use the default one * it will set the $this->array variable to whatever array is given in the construct * How the array works like a database; array('column_name' => 'column_data') */ function __construct($array = array('fruit' => 'apple', 'vegetable' => 'cucumber')) { $this->array = $array; } /* * Loops through the array and sets new variables within the class * it returns $this so that you may chain the method. */ public function execute() { foreach($this->array AS $key => $value) { $this->$key = $value; //we create a variable within the class } return $this; //we return $this so that we can chain our method.... } } That's all there is to this example class, now on how to use it. $chaining = new ClassName(array('website' => 'phpfreaks.com', 'rating' => 'awesome!')); //set up echo $chaining->execute()->website,'<br/>'; //will print 'phpfreaks.com' followed by a break //or you could do it this way $vars = $chaining->execute(); echo $vars->website,'<br/>'; //will print 'phpfreaks.com' followed by a break echo $vars->rating; //will print 'awesome!' I hope this helps anyone that might be having problems this is the first tutorial I have ever tried to write lol, so I hope it's clear enough. Point out any errors, and any ways in which it might be able to be improved. Add on to it if you'd like. Link to comment https://forums.phpfreaks.com/topic/166550-php5-chaining-variable-snippettutorial/ Share on other sites More sharing options...
trq Posted July 19, 2009 Share Posted July 19, 2009 Um, this is pretty standard stuff. Link to comment https://forums.phpfreaks.com/topic/166550-php5-chaining-variable-snippettutorial/#findComment-878327 Share on other sites More sharing options...
xnowandtheworldx Posted July 20, 2009 Author Share Posted July 20, 2009 Um, this is pretty standard stuff. Yes, I know it's standard stuff. The point was I had tried to figure out on how to do it, but, of course, I didn't know until about...5 days ago that you could create new variables on the fly inside of a class. There were no other explanations for this, so if it could help someone out that was stuck, like me, then that's all I wanted to do. Link to comment https://forums.phpfreaks.com/topic/166550-php5-chaining-variable-snippettutorial/#findComment-878879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.