I start my journey of getting to know PHP with my mentor Italo. He shows me the way, gives me a resources list, puts me in front of that dark cave where Mr. PHP lives, and prays for me to come out in one piece.
So, here I am, full-armed, setting my foot in Mr. PHP’s territory.
After doing some echo exercise in PHP’s cave, I am actually getting bolder and ready to kick some ass. I brought my square brackets with me, although I always like curly braces, they’re nicer. “You can take curly braces if you want.” PHP says. “What? I thought you were strict on this.” He shakes his head from side to side. Well, I guess PHP is more flexible than I thought, square brackets can be replaced by curly braces.
Now between the curly braces, PHP grabs me a bat, tells me to create 100,000,000 same bats. “What? How? I didn’t major in biology.” There he takes out a copy machine called loop, and while he puts the bat inside loop, foreach $bat, echo 100,000,000 same bats.
“Wow! That’s why you never starve to death even you’re living in a cave!” This evening, we have BBQ bats for dinner.
I knew there’s no such thing like free dinner at PHP’s. Some tough work is surely waiting for me as I’m getting full and burping a little. “Do you think the copy machine is working fine?” PHP asks casually. “What? Why not? It just copied 100,000,000 bats.” I glance at him, got my mouth full of bat meat. “Are you sure it was 100,000,000? Did you count them?” he asks. “No.” I take a quick look at the bat mountain lying next to the machine. How am I supposed to count that? It’s like counting stars in the night sky. “Do count the rest of the bats, while I’m still eating.” he ordered. Now I know what he wants. I grab my curly braces, standing between them, and start my function. If $bats > 90,000,000, elseif $bats > 91,000,000, elseif $bats > 92,000,000, elseif $bats > 93,000,000…
“I got it. 94,000,000 bats left!” Just as I show him the number, he pulls out a bat leg bone off his mouth and tosses it aside. “93,000,000!” I correct it quickly.
“Silly.” he said, ”You can just echo count($bats).”
Now that I know I can not only count $bats, but also check their $length by using strlen, $partial to some of them with substr, and make them bigger or smaller by using strtoupper and strtolower. Now all the bats look so different. But the math hasn’t done yet. While PHP is till eating, he likes to tear the bats apart, there are a number of bats have missing legs or arms that when I echo my count, it’s actually 93,165,567.26548967382. But it’s okay, I round the decimals to only 2 places.
“Now you get your reward.” he passes me a medium well bat, and asks, ”Now what do you do?”
“Hmm… say thank you?” I open my mouth and start biting the tiny skinny leg.
“And?” he sounds serious.
“$bats –;” I do remember to add a semicolon.
There he finally stops staring at me.
Lying down in PHP’s cave, the night is getting cold and there’s no window for me to see the sky.
“What do you want to be when you get out of here?” PHP asks, his voice is deep as this starless cave.
“I’ve always wanted to be a property creator.” I start to talk about my childhood dreams, “I like creating variables and tie them to objects. Like right now, I would really want to create a class under my name, between curly braces, public $isAlive = true; And print it to my mentor Ítalo, he is worried and waiting for me outside.”
“That’s it?” he asks, “You don’t want to be a function creator?”
“Yes, but there’re too many functions. Sometimes I return one and forget the other. I guess I’m better at naming variables.”
“Well, if you like naming, why don’t you organize the functions and give each of them a name. There you can call them when you need them, and you can even assign different values to different variables.”
“Cool! That would be my perfect future job. What is that called?”
“Object-oriented programming.” he says.
Since I’ve spent the whole last night thinking about printing a message to the outside, I woke up early this morning and sneaked out of bed. I grabbed my curly braces, and as I’m creating a class that publics my variable, the property passes through PHP’s bed and he overrides my object.
“False.” he creates a subclass and extends mine, and changes $isAlive = false;
“No!” I create another to override his, and he changes again.
I try to use a lot of ifs and elseifs to distract him, but no matter what I echo or print, he overrides all of them and destroy every single one of my objects.
“This is final!” I get so mad. I snap at him and final public my function. The function passes him and goes straight towards the gate.
“I got it.” I start to create final public functions and static functions with const variables, and he can’t do anything about them.
“You’ve beaten me.” PHP tosses me my knot bag and says, “you can go.”
I grab my knot bag, pack everything I’ve learned and create an associative array, echo ‘Goodbye’; and close ?>.
I see Ítalo is waiting for me at the gate as I step out of the cave.
“Did you get my message?” I show him my properties and methods. “He was trying to scare me. I don’t think he’s as scary as people say he is. He’s just a bit annoying sometimes. And his bats smelled terrible…”
As I’m talking, I turn my head and look back. I see PHP smiling at me.