Page 1 of 1

php classes

Posted: Tue Jul 19, 2005 8:12 pm
by playaz
Hi guys...

I have been using php for some time and I have developed a decent understanding, I'm now learning object orientated programming (OOP). Can someone explain the following code & how I can do the following in the most efficient way possible :
(i) - Initiate a new AutoMobile object, set the maximum speed to 200 & make it drive backwards.
(ii) - How would you remove all of the automobiles wheels?

Thanks in advance

Code: Select all

class AutoMobile { 
    var $Wheels; 
    var $Speed; 
    var $Direction; 

    function Car() { 
        $this->Wheels = 4; 
    } 
    function MaxSpeed($int) { 
        $this->Speed = $int; 
    } 
    function goBackwards() { 
        $this->Direction = ‘backwards’; 
    } 
    function setWheels($int) { 
        $this->Wheels = $int; 
    } 
}

Posted: Tue Jul 19, 2005 9:51 pm
by hawleyjr
Sounds like a homework assignment to me...?

Posted: Tue Jul 19, 2005 9:53 pm
by playaz
hawleyjr wrote:Sounds like a homework assignment to me...?
Not really homework as such, left school years ago! :) Just trying to get used to php5 and OOP in general.

Posted: Tue Jul 19, 2005 10:24 pm
by infolock
yourclass.php

Code: Select all

<?php
class AutoMobile { 
    var $Wheels; 
    var $Speed; 
    var $Direction; 

    function Car() { 
        $this->Wheels = 4; 
    } 
    function MaxSpeed($int) { 
        $this->Speed = $int; 
    } 
    function goBackwards() { 
        $this->Direction = ‘backwards’; 
    } 
    function setWheels($int) { 
        $this->Wheels = $int; 
    } 
}
?>
callingscript.php

Code: Select all

<?php
include('yourclass.php');
$a = new Automobile; //initiate the class
$a->MaxSpeed(200); //set max speed to 200
$a->goBackwards(); //make it go backwards
$a->setWheels(0); //remove all the wheels
?>