Professional Documents
Culture Documents
What is OOP?
What is an object?
The implementation of “Car” is hidden from us, however we can use its full
capacities.
OOP in PHP
PHP 5 (although most of the ideas in this article also apply to PHP 4) has
great support for object oriented programming by providing easy class
creation. PHP provides every paradigm other “true” OOP languages
implement (Python and JAVA, for example), like inheritance, polymorphism
and encapsulation.
Inheritance
The basic idea behind inheritance is that similar objects share common
properties. So by creating a “generic” class, we can have a blueprint to build
our subsequent classes on. Imagine, if you will, a car’s properties: color,
number of wheels, horsepower, number of seats, etc. Having this template
we can further specialize our cars by extending this class: creating a racing
car that has a “nitro” property, or a truck that has a “trailer” property. The
bottom line is: create a more generic class that contains most of the common
attributes and you will have much less work defining other objects only
slightly different. Instead of rewriting the whole code, you just extend it’s
properties, saving a lot of time in the process.
Encapsulation
Levels of visibility
Polymorphism
Ok, now for the real action. We will be creating a simple class to handle
image manipulation and information extraction. For the duration of this
tutorial I will assume you have a basic understanding of PHP (variables,
creating functions, working with control flow statements and loops). Reading
the GD PHP manual will remove any doubts you may have about image
handling functions.
1. <?php
2. class Image {
3. }
4. ?>
<?php
class Image {
?>
This just tells PHP we will start defining a new class, named “Image”. Now we
will define the class constructor. A constructor is simply the function that is
called when creating a new object. In PHP 5 this can be achieved by two
different methods: creating a public function with the exact same name of
the class (PHP 4 and on) or by creating a function called “__construct()” (PHP
5 only):
1. <?php
2. class Image {
5. }
6. }
8. ?>
<?php
class Image {
?>
1. <?php
2. class Image {
3. function __construct() {
5. }
6. }
8. ?>
<?php
class Image {
function __construct() {
}
$image = new Image(); // prints "We just created and object!"
?>
Class constructors should be used to ensure that the created object has a
minimal amount of data to work with; in our case, the desired image.
As such, the first thing we have to do is to read the image, whatever it’s type
may be. Currently, the GD library supports a number of image types, such as
jpg, png, gif, bmp, and others; we just have to read the image and determine
it’s type.
1. <?php
2. class Image {
3. function __construct($filename) {
4.
7. $buf = '';
8. while(!feof($fp))
10.
12. imagecreatefromstring($buf);
13. }
14. }
15. $image = new Image("image.png"); // If everything went well we have
now read the image
16. ?>
<?php
class Image {
function __construct($filename) {
$buf = '';
while(!feof($fp))
// create image
imagecreatefromstring($buf);
?>
So we try to open an image file and assign it’s file pointer to $fp, and, in case
of failure, terminate the program’s execution.
view plaincopy to clipboardprint?
1. $buf = '';
$buf = '';
…and read the whole file, concatenating the data read with our newly
created string contents.
1. while(!feof($fp))
while(!feof($fp))
Now we just have to create our image using the data we just read…
1. imagecreatefromstring($buf);
imagecreatefromstring($buf);
NOTE: jpg, png, gif and most image files need to be read in binary mode,
hence the “rb” passed as the second argument of the fopen function. “r”
means read-only and “b” means binary.
In it’s current form our class isn’t very useful. So we will be adding some
attributes and methods to make it more useful. So we will start by 1: defining
some internal variables (note the “private” visibility declaration before each
variable)
1. <?php
2. class Image {
3.
5. private $image;
6. private $width;
7. private $height;
8. private $mimetype;
9.
11.
15. while(!feof($fp))
17.
20.
26. }
27. }
29. ?>
<?php
class Image {
// class atributes (variables)
private $image;
private $width;
private $height;
private $mimetype;
function __construct($filename) {
$buf = '';
while(!feof($fp))
$this->image = imagecreatefromstring($buf);
$info = getimagesize($filename);
$this->width = $info[0];
$this->height = $info[1];
$this->mimetype = $info['mime'];
?>
And 2: a method to display the image.
1. <?php
2. class Image {
3.
4. .
5. .
6. .
7.
9. header("Content-type: {$this->mimetype}");
10. switch($this->mimetype) {
14. }
15. //exit;
16. }
17. }
19. ?>
<?php
class Image {
.
header("Content-type: {$this->mimetype}");
switch($this->mimetype) {
//exit;
?>
In this step we just created some class attributes (image, width, height and
mimetype) to hold the object’s data. Then we made some modifications to
assign the created image to our class attribute $image…
1. $this->image = imagecreatefromstring($buf)
$this->image = imagecreatefromstring($buf)
… and extracted the image’s informations to our remaining class variables
(read the documentation on getimagesize to fully understand how the image
information is read):
2. $info = getimagesize($filename);
3. $this->width = $info[0];
4. $this->height = $info[1];
5. $this->mimetype = $info['mime'];
$info = getimagesize($filename);
$this->width = $info[0];
$this->height = $info[1];
$this->mimetype = $info['mime'];
Next we created a function that outputs the image to the browser by defining
the appropriate headers (read more on http headers here) and using the
appropriate function (with the switch statement) to output the image based
on the original image mimetype (for this tutorial we will just support jpg, png
and gif but, as I said before, GD support a multitude of other formats. Read
the php documentation for more).
So what’s this “$this” stuff in there? “$this”, in PHP, refers to the class itself,
and it’s used to point to class attributes or functions. As such, $this->image
points to the class attribute named “$image” and $this->image = … changes
the value of the class attribute. If you were to write $image = … you would
just be creating a new local variable named “$image”, available exclusively
for the duration of the function. This is one of the main things to pay
attention to when creating classes in PHP.
Right now our class isn’t very useful. Sure, we can read our image and
display it, but that’s it. We will now create a sub-class to create our
thumbnails. (We really don’t need to create a sub-class, but we will for the
sake of the tutorial, to demonstrate inheritance and polymorphism). So, for
the inheritance to work properly we need to slightly change the definition of
our super-class (Image). We just need to change the visibility of our class
variables from “private” to “protected”. And now we will create the
constructor of our subclass.
1. <?php
2. class Image {
3.
5. protected $image;
6. protected $width;
7. protected $height;
8. protected $mimetype;
9.
10. .
11. .
12. .
13. }
14.
16.
18.
20. parent::__construct($image);
21.
26. }
27. }
28. ?>
<?php
class Image {
protected $image;
protected $width;
protected $height;
protected $mimetype;
.
.
parent::__construct($image);
$this->image = $thumb;
?>
So what exactly are we doing here? We created a new class, derived from our
original, meaning that we can access all of its public and protected attributes
and methods. We call the super-class constructor, responsible for reading the
image and extracting its information. The sub-class constructor does not call
its superclass constructor so we need to call it explicitly.
Now we create a new image for our thumbnail, with the width and height
passed:
Resample (resize) the original image into the new one, to create the
thumbnail:
And finally modifiy the original image to contain the thumbnail instead of the
full-size image:
1. $this->image = $thumb;
$this->image = $thumb;
And guess what? We don’t really need to write a new function to display the
thumbnail because the same principle applies, whether you are displaying a
full size image or a thumbnail. After all, it’s still an image! So we just need to
call our display() function, defined in the super-class, and we’re done!
Our completed class and respective sub-class.