Defining and Using Object Properties

Defining and Using Object Properties

Instantiate the Car Class

Some properties:

The Beetle's color is red.

The Mustang's manufacturer is Ford.

The $beetle Object:

Car Object
(
    [model] => 
    [color] => red
    [manufacturer] => Volkswagen
    [type] => 
    [_speed:Car:private] => 0
    [_extraData:Car:private] => Array
        (
        )

)

The $mustang Object:

Car Object
(
    [model] => 
    [color] => green
    [manufacturer] => Ford
    [type] => 
    [_speed:Car:private] => 0
    [_extraData:Car:private] => Array
        (
        )

)
Instantiate another car

I'm driving a blue Chrysler Dodge Caliber.

Top speed reached after acceleration:

Current speed: 10 mph
Current speed: 20 mph
Current speed: 30 mph
Current speed: 40 mph
Current speed: 50 mph
Current speed: 60 mph
Current speed: 70 mph
Current speed: 80 mph
Current speed: 90 mph
Current speed: 100 mph
Switch statement to reveal Car type
hatchbackInstantiate another car

I'm driving a red Volkswagen Beetle.

Stepping on the gas...
Current speed: 10 mph
Current speed: 20 mph
Current speed: 30 mph
Current speed: 40 mph
Current speed: 50 mph
Current speed: 60 mph
Current speed: 70 mph
Current speed: 80 mph
Current speed: 90 mph
Current speed: 100 mph

Top speed! Slowing down...
Current speed: 90 mph
Current speed: 80 mph
Current speed: 70 mph
Current speed: 60 mph
Current speed: 50 mph
Current speed: 40 mph
Current speed: 30 mph
Current speed: 20 mph
Current speed: 10 mph
Current speed: 0 mph

Stopped!

28
This car's MPG is: 28
Instantiate another car
green A cat is not a car. We try to use the paint() method to paint the cat
Instantiate the Garage Class
Lucky the cat is not a car object you should not be able to paint it, yet we did
The error stops all processing but doesn't show up otherwise.
Letting the compiler know by putting the type in the function declaration alerts the compiler

Instantiate the Car Class again
The car's color is aqua-marine
Instantiate the Car Class again

Some properties:

My car's manufacturer is Volkswagen.

My car's engine size is 1.8.

My car's fuel type is .

The $myCar Object:

Car Object
(
    [model] => Beetle
    [color] => red
    [manufacturer] => Volkswagen
    [type] => 
    [_speed:Car:private] => 0
    [_extraData:Car:private] => Array
        (
            [engineSize] => 1.8
            [otherColors] => Array
                (
                    [0] => green
                    [1] => blue
                    [2] => purple
                )

        )

)
Instantiate the Square class
Instantiate the ShapeInfo() class

The shape's color is green, and its area is 9.

Instantiate the Rectangle Class
Instantiate the ShapeInfo() class

The shape's color is yellow, and its area is 0.

Instantiate the Circle Class

My circle is red and its area is 50.265482457437.

Instantiate the Square

My square is green and its area is 9.

Instantiate the Polygon Class
Instantiate the ShapeInfo Class

The shape's color is yellow, and its area is 20.

Instantiate the Television Class
Instantiate TennisBall Class
Instantiate StoreManager Class

There are 100 42-inch televisions and 100 yellow tennis balls in stock.

Selling a television...

Selling two tennis balls...

There are now 99 42-inch televisions and 98 yellow tennis balls in stock.

Instantiate the Person Class

Harry Walters, age 28
Instantiate the Person Class

Instantiate the Person Class

Instantiate the Person Class

Serialize Person Object Harry

Harry is now serialized in the following string: 'O:6:"Person":6:{s:18:"Person_firstName";s:5:"Harry";s:17:"Person_lastName";s:5:"Wayne";s:12:"Person_age";i:28;s:3:"age";i:29;s:8:"frstName";s:13:"Harry Houdini";s:8:"lastName";s:23:"Magician Extraordinaire";}'
Converting 'O:6:"Person":6:{s:18:"Person_firstName";s:5:"Harry";s:17:"Person_lastName";s:5:"Wayne";s:12:"Person_age";i:28;s:3:"age";i:29;s:8:"frstName";s:13:"Harry Houdini";s:8:"lastName";s:23:"Magician Extraordinaire";}' back to an object...
UnSerialize Harry

Harry's age is: 29

Hary's name is: . Magician Extraordinaire .

Instantiate the User Class

The original user object:

User Object ( [username] => harry [password] => monkey [loginsToday] => 3 )

Serializing the object...

The user is now serialized in the following string:

O:4:"User":2:{s:8:"username";s:5:"harry";s:8:"password";s:6:"monkey";}

Converting the string back to an object...

Yawn... what's for breakfast?
Saving this object to the database...
The unserialized object:

User Object ( [username] => harry [password] => monkey [loginsToday] => )

 

CODE FOLLOWS

<?php
// Include the header file (assumed to contain common HTML and styles)
include('includes/header.php');
?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Defining and Using Object Properties</title>
        <link rel="stylesheet" type="text/css" href="css/common.css" />
    </head>
<body>
    <h1>Defining and Using Object Properties</h1>

    <?php
// Autoloader function to load class files dynamically
spl_autoload_register(function ($class_name) {
    include 
'classes/' $class_name '.php';
});

/*require_once( "classes/Person.php" );
require_once( "classes/Car.php" );
require_once( "classes/Garage.php" );
require_once( "classes/Shape.php" );

require_once( "classes/Television.php" );
require_once( "classes/Figure.php" );
require_once( "classes/Polygon.php" );
require_once( "classes/Rectangle.php" );

require_once( "classes/Sellable.php" );
require_once( "classes/ShapeInfo.php" );
require_once( "classes/Square.php" );
require_once( "classes/User.php" );

require_once( "classes/Circle.php" );
require_once( "classes/TennisBall.php" );
require_once( "classes/StoreManager.php" );*/

    // Car class





echo "Instantiate the Car Class <br />";
$beetle = new Car();
$beetle->color "red";
$beetle->manufacturer "Volkswagen";

$mustang = new Car();
$mustang->color "green";
$mustang->manufacturer "Ford";

echo 
"<h2>Some properties:</h2>";
echo 
"<p>The Beetle's color is " $beetle->color ".</p>";
echo 
"<p>The Mustang's manufacturer is " $mustang->manufacturer ".</p>";
echo 
"<h2>The \$beetle Object:</h2><pre>";
print_r$beetle );
echo 
"</pre>";
echo 
"<h2>The \$mustang Object:</h2><pre>";
print_r$mustang );
echo 
"</pre>";

echo 
"Instantiate another car <br />";
    
$myCar = new Car();
    
$myCar->model "Dodge Caliber";
    
$myCar->color "blue";
    
$myCar->manufacturer "Chrysler";
    
$myCar->type Car::HATCHBACK;

    echo 
"<p>I'm driving a $myCar->color $myCar->manufacturer $myCar->model.</p>";
    echo 
"<p>Top speed reached after acceleration:</p>";

    while (
$myCar->accelerate())
    {
    echo 
"Current speed: " $myCar->getSpeed() . " mph<br />";
    }

echo 
"Switch statement to reveal Car type <br />";
switch ( 
$myCar->type ) {
    case 
Car::HATCHBACK:
        echo 
"hatchback";
        break;
    case 
Car::STATION_WAGON:
        echo 
"station wagon";
        break;
    case 
Car::SUV:
        echo 
"SUV";
        break;
}

echo 
"Instantiate another car <br />";
$myCar = new Car();
$myCar->color "red";
$myCar->manufacturer "Volkswagen";
$myCar->model "Beetle";

echo 
"<p>I'm driving a $myCar->color $myCar->manufacturer $myCar->model.</
p>"
;

echo 
"<p>Stepping on the gas...<br />";

while ( 
$myCar->accelerate() )
{
    echo 
"Current speed: " $myCar->getSpeed() . " mph<br />";
}
echo 
"</p><p>Top speed! Slowing down...<br />";

while ( 
$myCar->brake() )
{
    echo 
"Current speed: " $myCar->getSpeed() . " mph<br />";
}

echo 
"</p><p>Stopped!</p>";

echo 
Car::calcMpg168) . " <br />"// Displays "28"

echo Car::displayMpg168) . " <br />"// Displays "This car's MPG is: 28"

echo "Instantiate another car <br />";
$car = new Car;
$garage = new Garage;
$car->color "blue";
$garage->paint$car"green" );
echo 
$car->color// Displays "green"

echo " A cat is not a car.  We try to use the paint() method to paint the cat <br />";
$cat "Lucky";

echo 
"Instantiate the Garage Class <br />";
$garage = new Garage;
//$garage->paint( $cat, "red" ); // Error!
echo $cat " " '  ' "  the cat is not a car object you should not be able to paint it, yet we did" "<br />";
echo 
" The error stops all processing but doesn't show up otherwise. <br />";
echo 
" Letting the compiler know by putting the type in the function declaration alerts the compiler" "<br /><br />";

echo 
"Instantiate the Car Class again<br />";
$car = new Car;
$x $car->color "aqua-marine"// Displays "The value of 'color' was requested"
echo "The car's color is $x <br />"// Displays "The car's color is blue"

echo "Instantiate the Car Class again <br />";
$myCar = new Car();
$myCar->manufacturer "Volkswagen";
$myCar->model "Beetle";
$myCar->color "red";
$myCar->engineSize 1.8;
$myCar->otherColors = array( "green""blue""purple" );

echo 
"<h2>Some properties:</h2>";
echo 
"<p>My car's manufacturer is " $myCar->manufacturer ".</p>";
echo 
"<p>My car's engine size is " $myCar->engineSize ".</p>";
echo 
"<p>My car's fuel type is " $myCar->fuelType ".</p>";

echo 
"<h2>The \$myCar Object:</h2><pre>";
    
print_r$myCar );
echo 
"</pre>";

echo 
"Instantiate the Square class  <br />";
    
$mySquare = new Square;
    
$mySquare->setColor"green" );
    
$mySquare->makeHollow();
    
$mySquare->setSideLength);

echo 
" Instantiate the ShapeInfo() class <br />";
    
$info = new ShapeInfo();
    
$info->setShape$mySquare );
    
$info->showInfo(); // Displays "The shape's color is green, and its area is 9."


echo " Instantiate the Rectangle Class <br />";
    
$myRect = new Rectangle;
    
$myRect->setColor"yellow" );
    
$myRect->fill();
    
$myRect->setWidth);
    
$myRect->setHeight);

echo 
" Instantiate the ShapeInfo() class <br />";
    
$info = new ShapeInfo();
    
$info->setShape$myRect );
    
$info->showInfo();






echo 
"Instantiate the Circle Class <br />";
    
$myCircle = new Circle();
    
$myCircle->setColor("red");
    
$myCircle->fill();
    
$myCircle->setRadius(4);

echo 
"<p>My circle is " $myCircle->getColor() . " and its area is " $myCircle->getArea() . ".</p>";

echo 
"Instantiate the Square <br />";
    
$mySquare = new Square();
    
$mySquare->setColor("green");
    
$mySquare->setSideLength(3);
echo 
"<p>My square is " $mySquare->getColor() . " and its area is " $mySquare->getArea() . ".</p>";


echo 
"Instantiate the Polygon Class <br />";
$myRect = new Polygon;
$myRect->setColor"yellow" );
$myRect->fill();
$myRect->setWidth);
$myRect->setHeight);

echo 
"Instantiate the ShapeInfo Class <br />";
$info = new ShapeInfo();
$info->setShape$myRect );
$info->showInfo();  // Displays "The shape's color is yellow, and its area is 20."


echo "Instantiate the Television Class <br />";
    
$tv = new Television;
    
$tv->setScreenSize42 );

echo 
"Instantiate TennisBall Class <br />";
    
$ball = new TennisBall;
    
$ball->setColor"yellow" );

echo 
"Instantiate StoreManager Class <br />";
    
$manager = new StoreManager();
    
$manager->addProduct$tv );
    
$manager->addProduct$ball );
    
$manager->stockUp();

echo 
"<p>There are "$tv->getStockLevel() . " " $tv->getScreenSize();
echo 
"-inch televisions and " $ball->getStockLevel() . " " .
        
$ball->getColor();
echo 
" tennis balls in stock.</p>";
echo 
"<p>Selling a television...</p>";
    
$tv->sellItem();
echo 
"<p>Selling two tennis balls...</p>";
    
$ball->sellItem();
    
$ball->sellItem();
echo 
"<p>There are now "$tv->getStockLevel() . " " $tv->getScreenSize();
echo 
"-inch televisions and " $ball->getStockLevel() . " " .
        
$ball->getColor();
echo 
" tennis balls in stock.</p>";

echo 
"Instantiate the Person Class <br /><br />";
    
$p1 = new Person"Harry""Walters"28 );
    
$p1->showDetails();  // Displays "Harry Walters, age 28"

echo "Instantiate the Person Class <br /><br />";
    
$p2 = new Person("Ann""Margaret"35);
   
// unset( $p2 );

echo "Instantiate the Person Class <br /><br />";
    
$p3 = new Person("Steve""Martin""40");
    
//die( "Something's gone horribly wrong!<br />");
echo "Instantiate the Person Class <br /><br />";
    
$harry = new Person("Harry""Wayne"28);
    
$harry->age 29;
    
$harry->frstName "Harry Houdini";
    
$harry->lastName "Magician Extraordinaire";

 echo 
"Serialize Person Object Harry <br /><br />";
    
$harryString serialize$harry );
echo 
"Harry is now serialized in the following string: '$harryString'<br />";
echo 
"Converting '$harryString' back to an object...<br />";
    
$obj unserialize$harryString );

echo 
"UnSerialize Harry <br /><br />";
echo 
"Harry's age is: $obj->age<br /><br />";
echo 
"Hary's name is: $obj->firstName . $obj->lastName . <br /><br />";

echo 
"Instantiate the User Class <br /><br />";
$user = new User;
$user->username "harry";
$user->password "monkey";
$user->loginsToday 3;
echo 
"The original user object:<br /><br />";
print_r$user );
echo 
"<br /><br />";


echo 
"Serializing the object...<br /><br />";
$userString serialize$user );
echo 
"The user is now serialized in the following string:<br /><br />";
echo 
"$userString<br /><br />";
echo 
"Converting the string back to an object...<br /><br />";
$obj unserialize$userString );
echo 
"The unserialized object:<br /><br />";
print_r$obj );
echo 
"<br /><br />";


// Include the footer file (assumed to contain closing HTML and scripts)
include('includes/footer.php');


?>

Saving this object to the database...
Saving this object to the database...
Saving this object to the database...
Saving this object to the database...