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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.