Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Object-Oriented Programming In Perl (part 2)
Object methods and properties, inheritance, overrides and destructors - they're all here!

| Ferrari Or Porsche? |

Now let's toss something new into the mix, by adding a few object properties to the constructor.


# this is Automobile.pm
# a Perl module to create new automobiles

# constructor
sub new
{
my $this = {};

# object properties
$this->{colour} = undef;
$this->{make} = undef;
$this->{shape} = undef;

bless $this;
return $this;
}

# other subroutines


Once you've added the variables to the constructor, you also need a way to initialize them - which is where the init() subroutine comes in. Let's add a bit of code to that, shall we?


# this is Automobile.pm
# a Perl module to create new automobiles

# other subroutines

sub init
{
my $this = shift;
$this->{color} = shift;
$this->{make} = shift;
$this->{shape} = shift;
}

# other subroutines


And now let's modify the test script - I'll use the init() method to create two completely different Automobiles.


#!/usr/bin/perl

use Automobile;

# create objects
$his = new Automobile;
$his->init("black","Ferrari","cigar");

$hers = new Automobile;
$hers->init("silver","Porsche","torpedo");

# run a few object methods

# let's take this baby out for a spin
$his->start();

# and so on


This time around, once an object is created, the init() method sets up some default properties like shape, make and colour. (You're probably wondering what all the shift()-ing is about in the subroutines above - don't worry about it for the moment, I've explained it all on the next page.)

And finally, how about doing something with the properties sent to the init() method? Let's add one more method, which lets you brag about your new car (and demonstrate how object properties can be used, too!)


# this is Automobile.pm
# a Perl module to create new automobiles

# other subroutines

sub brag
{
my $this = shift;
print "Guess what? I've just got myself a brand-spanking-new $this->{color}
$this->{make}, shaped like a $this->{shape}. Sure, I'll take you for a ride
in it. Call me."
}

# other subroutines


And let's make a final modification to our test script:


#!/usr/bin/perl

use Automobile;

$his = new Automobile;
$his->init("yellow", "Lamborghini", "missile");
$his->brag():


And here's what you should see when you run it:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
Guess what? I've just got myself a brand-spanking-new yellow Lamborghini, shaped like a missile. Sure, I'll take you for a ride in it. Call me.
').'
'

And, as you'll see if you experiment a little, you can create as many Automobiles as you like, each with different properties, and the brag() method will display an appropriate message for each one. Ain't OOP cool?


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback