...

View Full Version : Getting my head around OOP



gnolan
05-23-2011, 08:03 PM
I've been learning PHP for the past few weeks. All was going quite well until the introduction of Classes, Objects and all that goes with them. I had the same problem when i was learning C++ many years ago. I've got some kind of mental block when it comes to this area.

Can anyone prescribe some useful reading for me? Even better would be if someone could describe to me in layman's terms the idea behind classes and objects, the correct terminology, examples of where they are used and why?

I'm currently reading "Learning PHP, MySQL and Javascript" and looking at phpmanual.net when i run into something awkward; but i've reached a stumbling block here. Would appreciate some help.

Fumigator
05-23-2011, 09:44 PM
Being an old-time programmer from a COBOL-centric mindset, I too have struggled with OO, first understanding the _how_, and then with the _why_. I'm still inclined to ignore most of OO when I program which I admit is a weakness.

This book was good:

http://amzn.to/jyD7WM

I read another book which catered to a COBOL background which is my favorite OO conecpts book, but it's out of print apparently. Another sign the dinosaurs are retiring!

If it makes you feel any better, you can code PHP completely and satisfactorily and ignore the OO side. In fact, OO was kind of shoe-horned into PHP, so it works fine just using a function-based technique. Much more important than using OO is to separate the presentation layer from the function layer. PHP can get really messy really fast if you imbed everying right within your HTML markup. Ew.

Another suggestion I have: Install, learn, and use CodeIgniter to build a simple website. It's a PHP framework that uses classes/objects. For me it was helpful because I was able to see and work within a practical application of OO.

The LEAST helpful OO teaching tool (and I bristle every time I see this) is to use some abstract impractical example to explain how OO works. Class "dog"? Attribute "fur"? class "Horse" extends class "dog"? WTF people, give me an example that I can apply to a real-life programming situation.

gnolan
05-23-2011, 10:41 PM
Being an old-time programmer from a COBOL-centric mindset, I too have struggled with OO, first understanding the _how_, and then with the _why_. I'm still inclined to ignore most of OO when I program which I admit is a weakness.

This book was good:

http://amzn.to/jyD7WM

I read another book which catered to a COBOL background which is my favorite OO conecpts book, but it's out of print apparently. Another sign the dinosaurs are retiring!

If it makes you feel any better, you can code PHP completely and satisfactorily and ignore the OO side. In fact, OO was kind of shoe-horned into PHP, so it works fine just using a function-based technique. Much more important than using OO is to separate the presentation layer from the function layer. PHP can get really messy really fast if you imbed everying right within your HTML markup. Ew.

Another suggestion I have: Install, learn, and use CodeIgniter to build a simple website. It's a PHP framework that uses classes/objects. For me it was helpful because I was able to see and work within a practical application of OO.

The LEAST helpful OO teaching tool (and I bristle every time I see this) is to use some abstract impractical example to explain how OO works. Class "dog"? Attribute "fur"? class "Horse" extends class "dog"? WTF people, give me an example that I can apply to a real-life programming situation.

It's good to know that i'm not the only one who finds it a bit of a struggle. I think i'm really missing something basic about it, that if someone could point out to me i can't help but feel that everything would click.

To be honest it's not something that i'd be content to do without. On the contrary it's something that i'd like to master.

I'll take a look at the book you mentioned and maybe some other aimed specifically at OOP.

I've come across some of those examples you mentioned above, the likes of: Class Bear extends Class polarBear - PHP for zoologists.

Fou-Lu
05-24-2011, 12:42 AM
I think a bigger part of the problem when searching for a reasonable example is knowing exactly what it is you are looking for. Generic tutorials will always have arbitrary examples (though horse extends dog is really the stupidest thing I've ever heard since it doesn't make any sense whatsoever lol). PHP4's OOP was a hack in, thats for sure. The 5.x engine though is far more robust, and minus an entry point PHP could actually be viewed as a fully object oriented language (I'll never see it that way, and *may* consider it when an entry point is provided :P).
Shapes I find to be the norm:


interface IShape
{
public function getArea();
public function getPermiter();
public function getColour();
}


Coming from procedural languages as well created a stumbling block when I first learned OO - especially in PHP. PHP is in fact one of the worst languages to learn OOP imho. Its primary problem is its complete lack of explicit datatyping, and without this datatyping there is a lot of reasoning lost when attempting to describe PHP in an OO fashion. This example is completely useless:


class Car
{
public $colour;
public $make;
public $model;
...
}

Public in PHP = very bad. Always. Unlike C++, C#, java etc, that lack of datatype now means I can put whatever I want into a class which completely defeats the point; PHP must be provided with accessors and mutators for any member you want to expose. From members to method returns and arguments, no datatyping is an extreme problem.

OOP is something that seems to just 'click' and then you can program without any problem in it. Its the shift from the whole data in > process > data out into the paradigm of Object change that is the tricky part.

I'd go back to C++ or look into C#/Java to learn OOP. These are much tighter languages, so mastering these will make PHP OO a breeze. PHP's OO follows closely to the C#/Java syntax and overall usage, so I'd suggest one of these two. Just go in knowing that interfaces are King, and you will come out laughing (C# and java do not allow multiple inheritance like C++ does). That and treat every class as a datatype, and away you go.

bullant
05-24-2011, 01:07 AM
Fumigator is correct when he says that you can get by without OOP in PHP but I was the other way round when I got my head around the concept of OOP which is similar in all programming languages. I now tend to use OOP wherever practicable.

A class (or object) is a standalone piece of code that contains the properties and methods (call them functions if you prefer) to manipulate those properties.

Hopefully these "KISS" demos might help you understand the basics of a class.

The demos create a php class to create rectangles. The class has properties to store the length and width of each rectangle and methods to set and retrieve the length and width and calculate the area of each rectangle.

The first demo shows how to create a rectangle class and then test drive it by changing lengths and widths and outputing the length, width and areas. The demo creates 2 rectangles using the single class.

The second demo shows how you can then use the rectangle class to dynamically create rectangular divs in the html and style them according to the properties of each created rectangle and then output the area of each div in the html.

If you view the source html of demo 2 in your browser, you will see the css and html code that is created by the php code.

demo 1


<?php

class rectangle {

protected $length;
protected $width;
protected $fillColor;

public function __construct($length, $width, $fillColor) {
$this->length = $length;
$this->width = $width;
$this->fillColor = $fillColor;
}

// Accessor methods/functions
public function getLength() {
return $this->length;
}

public function getWidth() {
return $this->width;
}

public function getFillColor() {
return $this->fillColor;
}

//Mutator methods/functions
public function setLength($length) {
$this->length = $length;
}

public function setWidth($width) {
$this->width = $width;
}

public function setFillColor($fillColor) {
$this->fillColor = $fillColor;
}

public function getArea() {
return $this->length * $this->width;
}

}

//end of class
// code to test drive the class
$rect1 = new rectangle(100, 200, 'red');
$rect2 = new rectangle(70, 80, 'green');
//now display the area of each rect
echo 'area of rect 1 = ' . $rect1->getArea() . '<br />';
echo 'area of rect 2 = ' . $rect2->getArea() . '<br />';
//now change the length of each of the 2 rectangles
$rect1->setLength(50);
$rect2->setLength(80);
//now display the area of each rect
echo '<br />area of rect 1 with new length = ' . $rect1->getArea() . '<br />';
echo 'area of rect 2 with new length = ' . $rect2->getArea() . '<br />';
?>
demo 2


<?php

class rectangle {

protected $length;
protected $width;
protected $fillColor;

public function __construct($length, $width, $fillColor) {
$this->length = $length;
$this->width = $width;
$this->fillColor = $fillColor;
}

// Accessor methods/functions
public function getLength() {
return $this->length;
}

public function getWidth() {
return $this->width;
}

public function getFillColor() {
return $this->fillColor;
}

//Mutator methods/functions
public function setLength($length) {
$this->length = $length;
}

public function setWidth($width) {
$this->width = $width;
}

public function setFillColor($fillColor) {
$this->fillColor = $fillColor;
}

public function getArea() {
return $this->length * $this->width;
}

}

//create an array of rectangle objects
$rectangles = array();
$rectangles[] = new rectangle(100, 200, 'red');
$rectangles[] = new rectangle(70, 80, 'green');
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css">
<?php
$count = 1;
foreach ($rectangles as $rect) {
echo '#div' . $count++ . '{';
echo 'width: ' . $rect->getWidth() . 'px;';
echo 'height: ' . $rect->getLength() . 'px;';
echo 'background-color: '.$rect->getFillColor().';';
echo 'border: 1px solid black;';
echo '}';
}
?>
</style>
</head>
<body>
<!-- create divs withe the properties of each rectangle -->
<?php
$count = 1;
foreach ($rectangles as $rect) {
echo '<div id="div' . $count++ . '">Area = ' . $rect->getArea() . '</div>';
}
?>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum