-2

I am new to PHP. I am using a MVC project as an example, and I noticed that each time a submit button is pressed my controller is called. The issue with this is that it creates a new model every time the button is pressed.

To fix this, I used a hidden field to check if the button has already been pressed. If it has, then I do not instantiate a new Model, otherwise I do. Code from controller is below:

 //code listed below is in the controller which is called each time button
 //has been pressed...
 $myModel;//used to access model and its functions from controller
 if(isset($_POST['has_started'])) 
 {
     //stores some logic that uses $myModel variable
     playingGame();
 }
 else
 {
   echo "just starting...";
   $myModel=new HangManModel();
   startGame($myModel);
 }

This seems to work, BUT then when playingGame() is called it tells me I cannot use $myModel and its functions because it was not declared, or it is a non-object. How can I fix this? Thanks for the help!

tereško
  • 58,060
  • 25
  • 98
  • 150
JKo
  • 1,260
  • 1
  • 10
  • 18
  • What is "MVC project", since you code has **NOTHING** to do with MVC architectural pattern ?! – tereško Sep 02 '14 at 05:15
  • I think you have the wrong idea here. PHP is intended for use over HTTP, and HTTP is stateless. This means you will have to instantiate your controller on every request. It should also be noted that "MVC" in php is not really MVC at all (atleast what you would find in something like C) – Scopey Sep 02 '14 at 05:16
  • How does it have nothing to do with MVC? The code above is in my controller class. I am trying to figure out how to prevent Model from being created each time 'submit' is pressed. – JKo Sep 02 '14 at 05:17
  • @Scopey Thanks, that is much more clear for me. However, a model cannot be recreated each time. How can this be solved? – JKo Sep 02 '14 at 05:20
  • @JacobKoko just because you call something in your code "controller" or "model" does not turn it magically in MVC architecture. – tereško Sep 02 '14 at 14:33
  • @tereško Thanks for the input, but that does not help me at all. I am not showing all of my code which may have you confused. I have a Model which stores the logic of the game, a View which displays the GUI, and a controller which acts as a traffic director between the two.The model variable in my code above is an instance of my Model class, which stores only logic. – JKo Sep 02 '14 at 14:38
  • That is **not** how information flow in MVC architectural design. Also, in PHP, after each execution, the "application" is destroyed. Which means that you **have to** recreate the object and have to restore data from some form of storage. Preferably: session. – tereško Sep 02 '14 at 14:44
  • Okay, thank you for the input. @Scopey said the same exact thing in the comments listed above. I have used MVC in the past, but not in regards to PHP. I am a university student, and I have been taught that MVC follows the pattern I described above. It separates the business logic from the view which is essentially the purpose of the pattern. – JKo Sep 02 '14 at 14:49
  • @tereško I read your answer in regards to MVC (http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000), and that makes things much more clear to me. It seems as though my past experiences with MVC in languages like Java/Objective-C have given me the wrong idea about MVC in regards to web languages like PHP. – JKo Sep 02 '14 at 15:03
  • If you want to understand the way how MVC pattern implements SoC, you should start by taking a look at wikipedia (focus on the diagram, which shows the information flow) and then read [this article](http://martinfowler.com/eaaDev/uiArchs.html) from M.Fowler. – tereško Sep 02 '14 at 16:47
  • @tereško Thanks for the help, I will check out that article for sure. – JKo Sep 03 '14 at 02:21

1 Answers1

1

First, there is no mvc pattern I see here. Secondly, php executes and generates the output in html and javascript and sent to the browser. At the next postback, php does not store any state for this.

As per my understanding, for storing states you can use one of the following.

1) Option1-->Using session.

//code listed below is in the controller which is called each time button
 //has been pressed...
 $myModel;//used to access model and its functions from controller

  if(!isset($_SESSION)){session_start();}
 if(isset($_POST['someuniqueID'])) 
 {
     //stores some logic that uses $myModel variable
     $someuniqueID = $_POST['someuniqueID'];
     $myModel = $_SESSION[$someuniqueID]; //retrieving the session object.
     playingGame($myModel); //passing your model to the main function.
 }
 else
 {


   echo "just starting...";
   $myModel=new HangManModel();
 //start session 

    $_SESSION['someuniqueID'] =  $myModel; // do not store very complex model object to avoid server memory problem.
   startGame($myModel);
 }

2) Option2--> you can serialise that object and send it in hidden field then it will be posted back but this will have bandwidth consumption problem. Each time, the page is posted, it will send your model back and forth.

3) Option3--> If your model is big, Use database and create a temp table where you can store unique id and serialised $myModel object. You will need to clean the unwanted states from database. because unlike session and hidden field it is permanent storage.

Ganesh Nemade
  • 1,504
  • 12
  • 17
  • Thank you very much! I have used the MVC design pattern numerous times but never in PHP. I will try this out, I appreciate the feedback. – JKo Sep 02 '14 at 05:35