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

| Building Your Very Own Automobile |

Before we get into the nitty-gritty of object methods and properties, I want to take a minute to make sure that we're all clear on the basics. And I'm going to use an example I've used before to communicate the concept to those of you who may still not be too comfortable with it.

Take your car, the vehicle that transports you from place to place, and consider this, within the framework of OOP, as an "object". This object has certain basic properties - shape, size, colour - and certain basic functions - start, stop, accelerate and decelerate.

Now take it one step further. Every car can, in fact, be considered a subset of the class Automobile, which defines the basic characteristics and functions of every automobile on the planet. Once the class Automobile spawns a new car, or "object", its individual characteristics (color, shape) can be changed without affecting either the parent class or other spawned objects.

In Perl, a class is equivalent to a package - and the package might look something like this:

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

package Automobile;

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

# subroutine (aka "object method")
sub init
# code to initialize variables (aka "object properties")
# this won't stay empty for long - keep reading!

# a few other object methods
sub start
# some code

# a few other object methods
sub stop
# some code

sub accelerate
# some code

sub decelerate
# some code

# remember to end the module with this

In order to use this, you simply need to instantiate a new object in your Perl script by calling the constructor, like this:


use Automobile;

# create objects
$his = new Automobile;
$hers = new Automobile;

# run a few object methods

# let's take this baby out for a spin

# well, hello there, pretty lady!
# care to join me?

# this is fun!

# was that a speed trap?

# busted!

You're already familiar with the method of creating a new object - the example above simply demonstrates the manner in which other methods can be applied to the object. These methods are nothing more than subroutines within the package definition.

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
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
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

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