johnnyjohnny Posted June 29, 2009 Share Posted June 29, 2009 I want to establish the similar setup Rails has, MVC, controller gets request, retrieves the model, view uses the model to output html. What is the best way to pass the model variable from the controller script to the view script? (view function is called within controller script) i.e. //controller <?php $model = new User(); $model->user_name = "johnnyjohnny"; $output_html(); ?> //view <?php function output_html() { echo "<div>$model->user_name</div>" } ?> how to pass model from controller to view? thanks in advance Link to comment https://forums.phpfreaks.com/topic/164161-php-variable-in-mvc/ Share on other sites More sharing options...
WolfRage Posted June 29, 2009 Share Posted June 29, 2009 That does work right? I think because you are using OOP that your technique is a solid and efficient way of passing the variable. Link to comment https://forums.phpfreaks.com/topic/164161-php-variable-in-mvc/#findComment-865989 Share on other sites More sharing options...
johnnyjohnny Posted June 29, 2009 Author Share Posted June 29, 2009 That's what I thought, but surprising no. Link to comment https://forums.phpfreaks.com/topic/164161-php-variable-in-mvc/#findComment-865995 Share on other sites More sharing options...
JasonLewis Posted June 29, 2009 Share Posted June 29, 2009 If the variable isn't already defined, I'm pretty sure you need to use the __set() magic method. public function __set($index, $value){ $this->$index = $value; } And what you have there doesn't look like OOP, unless you've just copied the functions from your code. Link to comment https://forums.phpfreaks.com/topic/164161-php-variable-in-mvc/#findComment-866010 Share on other sites More sharing options...
johnnyjohnny Posted June 30, 2009 Author Share Posted June 30, 2009 I don;t quite understand. p.s. it's not OOP. If the variable isn't already defined, I'm pretty sure you need to use the __set() magic method. public function __set($index, $value){ $this->$index = $value; } And what you have there doesn't look like OOP, unless you've just copied the functions from your code. Link to comment https://forums.phpfreaks.com/topic/164161-php-variable-in-mvc/#findComment-866041 Share on other sites More sharing options...
JasonLewis Posted July 1, 2009 Share Posted July 1, 2009 You're using it like OOP though. Do you understand how MVCs work? They can be pretty complicated. Link to comment https://forums.phpfreaks.com/topic/164161-php-variable-in-mvc/#findComment-867003 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.