| « Because sometimes you need a laugh | Banner ads can make you happy! » |
Sun, Nov 15, 2009
![[Icon]](rsc/img/chain_link.gif)
Object-oriented programming in PHP.
As somebody whose background in languages is mostly in C, the whole OOP thing is a bit of a mystery to me. I've yet to work out why an object is better than procedural functions & structs etc., but I'm sure that'll come in time.
But I've got a few O'Reilly books laying around on the subject of PHP, so I looked to them for an answer as to what OOP really *is*
For once, they were useless. Not only because they have bugger all in the way of explanations, but also because they have such godawful code examples.
It's the names they use. One book has the same name for an object as a function, so when they refer to 'Song' you can't tell if they're calling the object or the function IN the object.
The other has the same name for two different variables. So you get this dazzlingly-clear example:
$this->str = $str;
WHICH str is which?? What IDIOT thought this was a good example??
(Actually, I can answer that one - Rasmus Lerdorf, apparently, thought having two variables with the same name would make the example easy to follow)
So I sat down and worked through the creation of an object. There's absolutely nothing useful about the code I came up with, except for the fact that if you've no idea how PHP does OOP things, this'll make things a little bit clearer. So here it is, published online just on the offchance that anyone else wants to know just how the Hell to get started with objects in PHP.
Follow up:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PHP OOP test</title>
</head>
<body>
<?php
// Define a new object called "object_example"
class object_example {
//Define an object variable
var $object_variable = 'Object-defined variable';
//Define a function that modifies the object variable
function object_variable_rename_function($function_variable) {
$this->object_variable = $function_variable;
}
//Define a function to echo the object variable
function object_echo() {
echo $this->object_variable . '<br />';
}
}
//Right, having defined an object, let's make use of it!
//Create a new instance of the object & echo its default variable
$test_object = new object_example;
$test_object->object_echo();
//Use object's redefine function to change value of its variable
$test_object->object_variable_rename_function('Function-defined variable');
$test_object->object_echo();
//Use 'extends' to create a new object that inherits from our first object
class inheriting_object extends object_example {
function object_variable_length() {
echo 'The default string, <em>"' . $this->object_variable . '",</em> is ' . strlen($this->object_variable) . ' characters long.';
}
}
$test_extended_object = new inheriting_object;
$test_extended_object->object_variable_length();
?>
</body>
</html>
And the output will be (in W3C-compliant code, of course)
Object-defined variable
Function-defined variable
The default string, "Object-defined variable", is 23 characters long.
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12
Facebook Syndication Error
09/02/12
![]()
I last listened to:
Johann Pachelbel - Canon in D major
Most recent photo:
Submersible houseboat