Posts Tagged oop
Three Neat JavaScript Tips
Posted by Howard Yeend in javascript on September 2, 2011
JavaScript is a great language. It really can power your entire web stack from animated effects on the frontend to ajax calls for faster user experiences, to backend JSON APIs written in NodeJS. It’s been called the language of the web before, and never has it been more true than now.
I’d like to share some little techniques I’ve picked up which make your life easier as a professional JavaScript developer. Actually the first two are applicable to many languages.
Read the rest of this entry »
Poking holes in PHP object privacy
Posted by Howard Yeend in PHP on November 1, 2010
PHP provides a decent model of class member visibility, with public, private, and protected members to help you define tight APIs for your objects and show other developers how your object is supposed to be used. But used naively, PHP’s ‘magic methods’ can easily and subtly subvert this system, making everything public.
If you’re still new to object oriented programming in PHP5, think of “public” as roughly analogous to “my function’s arguments” and “private” as “local variables inside the function”. You wouldn’t want someone calling your function and messing with the local vars, and you wouldn’t want someone using your object messing with its private members.
Magic methods provide functionality like catching references to methods and properties which are not visible to us, and doing special things with them. Magic methods have always struck me as a bit weird, and whenever you bring them up in discussions online, there’s always a few people with reservations about them – efficiency, clarity, use-cases and so on.
I’m still in two minds; they can be useful in some circumstances, but here’s one reason why they could be considered harmful: Used carelessly, they can easily enable an OOP antipattern where all class members become public, even those declared as private or protected in the class definition. Read the rest of this entry »
Overloading in PHP
Posted by Howard Yeend in PHP on October 10, 2010
Murray Picton wrote up a blog post today on overloading functions in PHP. Overloading is a useful feature of many languages. Murray gives a nice definition in his post:
Overloading a function is the ability to define a function more than once with a different set of parameters for each one and then when it is called, the version of the function that matches the parameter set will be executed.
so you can define function foo(String $a) and foo(Array $a) and a different version will be called depending on how you call it. But, PHP’s idea of overloading is completely different and not really related.
Hello ladies, look at PHP, now back to Java, now back to PHP. Sadly, PHP’s not Java, but it can look (a bit) like Java if you stop using lady-scented functions and start using method overloading.
I just wanted to get that quote in somehow.
Murray’s solution is to use func_get_args inside your function, and perform some logic to switch execution to a different branch depending on what we find there. Something like:
function foo() {
$args = func_get_args();
if(is_string($args)) { /* do some stuff */ }
else if(is_array($args)) { /* do some stuff */ }
}
This works well enough, but there are a few things I don’t like about it:
- You have to put all the code into one function, instead of literally overloading multiple functions
- The “which version am I” code and the actual functionality are intermixed inside the function
I thought an OO solution might mitigate against these things, allowing us to write completely separate functions, but which can be called with the same name, a different one being executed depending on the arguments. I’m not certain my solution is an improvement over Murray’s, but it’s a different approach if nothing else:
PHP 5.3.0 Released!
Posted by Howard Yeend in PHP on July 1, 2009
If you’re still stuck in the old PHP 4 days, please please please take today’s release of PHP 5.3.0 as your cue to start learning about the wonderful world of object oriented PHP 5.
5.3.0 isn’t a hugely interesting release, but getting your PHP4 code compatible with 5 will ease the process when PHP 6 comes along. PHP6 is really where it’s at:
What’s out:
No more register_globals (finally)
No more magic_quotes (you kinda liked magic quotes? Admittedly it was handy, but when you think about it, having code that may or may not be sanitised depending on a php.ini setting is a Bad Ideaâ„¢)
No more HTTP_GET_VARS and cousins. Just change to $_GET etc and you’re fine.
What’s in:
A bunch of minor fixes aaand:
Namespaces – so we can section off bits of code properly. Woohoo! more info here.
Short post today I know, but I’ve got some real coding to do :) If you’re not doing anything better, go download PHP and have a play with it.
Recent Comments