php classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
playaz
Forum Newbie
Posts: 4
Joined: Tue Jul 19, 2005 8:02 pm

php classes

Post 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; 
    } 
}
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Sounds like a homework assignment to me...?
playaz
Forum Newbie
Posts: 4
Joined: Tue Jul 19, 2005 8:02 pm

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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
?>
Post Reply