Alex Rivera | Logout

Hidden Features of PHP?

Asked 2008-09-14T15:21:11.680
174

I know this sounds like a point-whoring question but let me explain where I'm coming from.

Out of college I got a job at a PHP shop. I worked there for a year and a half and thought that I had learned all there was to learn about programming.

Then I got a job as a one-man internal development shop at a sizable corporation where all the work was in C#. In my commitment to the position I started reading a ton of blogs and books and quickly realized how wrong I was to think I knew everything. I learned about unit testing, dependency injection and decorator patterns, the design principle of loose coupling, the composition over inheritance debate, and so on and on and on - I am still very much absorbing it all. Needless to say my programming style has changed entirely in the last year.

Now I find myself picking up a php project doing some coding for a friend's start-up and I feel completely constrained as opposed to programming in C#. It really bothers me that all variables at a class scope have to be referred to by appending '$this->' . It annoys me that none of the IDEs that I've tried have very good intellisense and that my SimpleTest unit tests methods have to start with the word 'test'. It drives me crazy that dynamic typing keeps me from specifying implicitly which parameter type a method expects, and that you have to write a switch statement to do method overloads. I can't stand that you can't have nested namespaces and have to use the :: operator to call the base class's constructor.

Now I have no intention of starting a PHP vs C# debate, rather what I mean to say is that I'm sure there are some PHP features that I either don't know about or know about yet fail to use properly. I am set in my C# universe and having trouble seeing outside the glass bowl.

So I'm asking, what are your favorite features of PHP? What are things you can do in it that you can't or are more difficult in the .Net languages?

Edit
Report

7 Answers

34

HEREDOC syntax is my favourite hidden feature. Always difficult to find as you can't Google for <<< but it stops you having to escape large chunks of HTML and still allows you to drop variables into the stream.

echo <<<EOM
  <div id="someblock">
    <img src="{$file}" />
  </div>
EOM;
answered 2009-06-22T19:52:46.347
16

Actually, you're not quite right about that you cannot specify what types a method expects, it does work as you'd expect.

function foo ( array $param0, stdClass $param1 );

Note: This only works for 'array' and object names.

And so on, and you can even pass in your own classes as expected parameters. Calling the methods/functions with something else will result in a fatal error.

Another hint about a good intellisense in PHP. We use ZendStudio and it will actually work a lot better if you write good PHPDocs for your methods, it will look into those when hinting.

answered 2008-09-14T19:15:01.167
13

Date functions. I have to handle a lot of time information and date strings all day long, so functions like strftime() and strtotime() are just awesome.

answered 2009-03-20T09:56:37.047
12

Error suppression via the error control operator, @, should almost never be used. It promotes lazy and non-defensive coding practices by simply ignoring errors, creates debugging nightmares since errors of all types--even fatal ones--will be suppressed, and, in some cases, can cause a hit to performance (especially when suppressing large quantities of errors).

answered 2009-06-21T22:12:05.257
5

preg_split(), array_intersect(), and array_intersect_key().

answered 2009-06-21T22:20:56.970
3

I have started to switch over to python, and one thing I loved in python is the live interpreter. It wasn't until working on a php project later that I realized php does have this option, it's just not widely known. In a command prompt, type php -a and paste in any php code you want to test, but just remember to start it with <?php

answered 2009-07-23T18:21:11.913
2

The predefined interfaces:

http://php.net/manual/en/reserved.interfaces.php

For example implementing ArrayAccess will make your object appear as an array or Iterator will allow it to be used in a foreach statement.

Unfortunately you can't use "object arrays" with the native functions that take arrays as parameters.

I also found it useful to override the __call function which allows you to dynamically create properties and methods for an object.

In my database abstraction I use this to generate functions that are named by the database column names. For example if there is a column 'name' then you can change values in it by using updateByName("foo").

answered 2010-01-09T06:23:23.410

Your Answer