| « 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.
Creative Hedgehog
Colmena colmena. (Quizá del celta *kolm?n?, der. de *k?lmos, paja; cf. bretón kôlôen-wénan, de kôlô, paja, y wénan, abejas). 1. f. Habitación natural de las abejas. 2. f. Enjambre que vive en la colmena. 3. f. Recipiente construido para habitáculo de las abejas. 4. f. Lugar o edificio en el que vive mucha gente apiñada. [...]
28/07/10 - SPN3730 vocabulario 2
Hari's corner
A few of my faves - please comment if you find them interesting
24/07/10 - Photos I've taken - my favourites
Place of Stuff
Joseph‘s story continues… Ten of his eleven brothers travel to Egypt to buy food to get them through the famine. Incidentally, something has occurred to me: in the tales of Joseph, God seems to be more bothered by getting Joseph into a position of power than in either preventing/alleviating the famine or in making the Israelites get [...]
24/07/10 - The Bible ? Joseph is an Evil Genius
Advice From a Single Girl
So Friday (last) started out so well, I knew it was going to be an awesome day.
I slept in (ahhh, bliss) and went for a morning walk to mail some....er...mail (because, seriously, what else can you mail? turtles?) and it was sunny and warm and I hadn't had any caffeine yet so I got myself a Slurpee. Nothing says awesome Summer day like a 10 am Coke Slurpee cooling you down in the sun.
But do you know what really tipped the morning into full-blown awesomeness? The two shirtless, amazingly hot guys who jogged past me, sweaty and gorgeous as I walked home. Ahhhhh, sugar, sun, and sexy, my own personal Summer trifecta.
I went over to where C-Dawg was staying and picked her up (so there would be no driving necessary) and we came back to my apartment, poured ourselves a summer-worthy drink and headed out on the town.
We wandered through downtown, people watching and talking and laughing and window shopping and then we headed to one of the local patios and ordered up a pitcher and some appetizers.
And that's when the real fun began.
You see, C-Dawg and I love people watching. And more than that, we love making up little stories about people and trying to guess who they are. We'd soon discovered that Friday would have to be known as "Everyone Looks Familiar Day" because I kept on seeing people that I thought looked familiar but I couldn't tell if they actually were or if I was just imagining it.
We decided that the couple next to us had just boated in on their yacht and that the guys across from us were all discussing their volleyball league's last game.
We also tried to narrow down which men C felt were too young for me and which she deemed "just right." Once we'd narrowed my age-group down to a ten year span she tested me to see if I could actually tell which guys were ok and which were in the "are you crazy, he's way too young" category.
I did not do well at this. (sigh)
As the pitcher got emptied, a table behind us became filled with a bunch of guys. C-Dawg, needing to "get out of the sun" (which we're pretty sure the guys could tell was an obvious ploy for her to be able to stare at the guys instead of having to pretend to look around and can I just say thank goodness for sunglasses and how easy they make it to check out cute guys?) sat next to me and we started to figure out the back story for these guys.
Later, C decided to choose which of the guys she'd set me up with and when she did she very kindly me that I could go out with the nice, sweet, geeky one because I'm a geek too at which point I protested until she promised she was a geek as well and it wasn't a bad thing. (Strangely enough I know what she means.)
At one point, the waiter came over and there'd been this on-going joke between the three of us because servers kept on trying to bring us food we hadn't ordered and I kept on making this dumb joke about it and then when C-Dawg told me the joke was getting old and the waiter laughed, I turned to him and said (and I quote) "Hey, I'm just going to keep saying it because it never be's not funny!"
At which point he suggested that this wasn't our first patio of the evening and I couldn't stop laughing because I couldn't believe I'd said "be's" and how as I'd said it it had TOTALLY been a word.
Ahhh alcohol, what silly things you do to my brain.
We hit up a few more places after that and went for dinner at my favourite place and then watched an awesomely bad movie back at my place. (Hi, I'm Victoria and I'm going to say the word 'place' as many times as possible in one sentence. I are a good writer.)
It was pretty darn awesome and I'm sure there's more I can think of, like how she wet-willied a statue and how she almost convinced me to give nice geek guy my number and how we sat outside the best ice cream place in town and convinced a bunch of other people that yes, they really should go inside and get a cone.
A good day, a great afternoon, a fun evening. It always be's like that with the C-Dawg. I can't wait til we get to do it again.
30/07/10 - It Never Be's
Nation
  This was possibly the most ridiculous show I have seen in a long time and I can get Sky 1 I know ridiculous. It could be summed up in three sentences Do you know what's in your cereal? Want to? Read the label. Instead it went on for a hour about how evil the [...]
27/10/09 - Dispatches ? do you know what?s in your breakfast? (warning...
Blogroll generated by MagpieRSS
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
Dominic just discovered that if you have two thousand mockingbirds, technically you've got two kilamockingbirds :).
30/07/10
![]()
I last listened to:
The Offspring - She's Got Issues
Most recent photo:
Submersible houseboat