Jump to content

OOP and MVC


keeps21

Recommended Posts

Hi

 

I'm trying to get my head around OOP and MVC.

 

I'm wondering whether I have it right with the following code - also attached as a zip file.

 

model.php

<?php 
class Student { 

   /* These are some of the properties */ 
   var $Name; 
   var $Gender; 
   var $GradeLevel; 
   var $Schedule; 

   /* Constructor. Called as soon as we create a student */ 
   function Student($studentName, $studentGender) { 
      $this->Name = $studentName; 
      $this->Gender = $studentGender; 
   } 

   function goToClass() { 
   } 

   function rideTheBus($busNumber) { 
      $statement = $this->Name . " rides bus number " . $busNumber; 
      return $statement; 
   } 

   function skipSchool() { 
   } 

   function changeGender() { 
      if ($this->Gender == "Female") { 
         $this->Gender = "Male"; 
      } else { 
         $this->Gender = "Female"; 
      } 
   } 
}
?> 

 

view.php

<?php 
include "model.php"; 
include "control.php";

print $a_student->Gender; 
print "<br>"; 
print $b_student->Gender; 
print "<br>"; 
print $c_student->Gender . " " . $c_student->Name; 

print $a_student->rideTheBus("65"); 

print $c_student->Gender; // this will print "Male" now... 

?> 

 

control.php

<?php 

$a_student = new Student("John","Male"); 
$b_student = new Student("Mary","Female"); 
$c_student = new Student("Larry","Female"); 

$c_student->changeGender(); 

?> 

 

Any advice would be greatly appreciated.

 

Thanks.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/129094-oop-and-mvc/
Share on other sites

1) Don't use PHP4.

 

You've lost me with this? I'm running 5.2.5.

 

2) It doesn't seem like MVC really...you'd usually call a specific controller method to handle certain models and views... >_>

 

Is there an example you could give me for this to explain what you mean?

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/129094-oop-and-mvc/#findComment-669374
Share on other sites

I meant "Don't use PHP4 OOP syntax".  It's horribly deprecated.  Look up PHP5 OOP syntax.

And check out how CakePHP and Zend Framework do their routing calls.  They use call_user_func_array() to call certain controller methods based on what's being asked for by the client.

Link to comment
https://forums.phpfreaks.com/topic/129094-oop-and-mvc/#findComment-669393
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.