PHP duplicate function name gives no errors

I had a very simple service class to perform some tasks:

class SomeService {
public function doSomething(){}
}

While adding some other functions, I was copy-and-pasting the doSomething(){} structure, and then renaming the new functions. I apparently forgot to rename one of the functions, and left it as doSomething(){}, which meant I had two functions with the same name.

This Service is being tested using SimpleTest framework for Unit Tests, and when running the tests via the browser, I saw nothing but a blank page. I searched through the PHP logs, nothing. Apache logs? Nothing.

After doing some debugging, I eventually narrowed it down to the SomeService class, which I promptly commented out everything, and I at least was able to see failures in my Unit Tests. After adding the functions back one by one, it become obvious of my goof, and once I removed the duplicate name all of my test passed again.

That was a few hours down the drain that could have been avoided with an extremely simple error message.

PHP System Variables

System variables in Java are pretty straight forward: add them with the -D parameter when you start the JVM (/bin/java -Dcom.example.prod=true). However in an interpreted language like PHP, it’s a little different: you update the php.ini file with your environment-specific key-value pairs.

So in PHP if you want to set com.example.debug=true, then you’d simply place it in the php.ini file, and restart the Apache that serves the PHP files.

I do not fully understand why yet, but to get the standard php.ini variables (e.g. post_max_size) you use the function ini_get(“post_max_size”), but if you want to retrieve a custom variable, you should use get_cfg_var(“my.custom.var”).

The reason you would want to use “System Variables” instead of a global value is that System Vars are pertinent at a higher level than the code. You would want to let the code know which environment it is running in at startup, outside of the code.

PHP Test Frameworks

I’ve got a PHP project that desperately needs some basic Unit Testing. I am a huge fan of building automated tests as I develop code, and (mostly) before I even write the implementations. “How do we test this?” is one of the 2 most important questions in software development. (The other being “How do we define ‘done'”)

JUnit has been amazing for my Java development, so having a PHP test framework that is similar, or based off of its ideas is a big plus if I can get it.

There are a lot of good options out there that look good, but I have not had a chance to review all of them yet. The main ones considered at this point are:

  1. SimpleTest
    • PROS: easy to get up and running, just download the tar.gz file, unzip, and go. It is also very JUnit like in the Java world (TestSuites, run functions that start with the word “test”, etc.).
    • CONS: latest version was released in 2012
  2. Testify.php
    • PROS: easy to get up and running
    • CONS: not updated since August of 2014, and does not seem to be as JUnit-like as I’d prefer, but maybe that is due to the lack of documentation, and my lack of time to research it
  3. PHPUnit – The PHP Testing Framework
    • PROS: most up-to-date and seems very enterprise ready (latest version was released about a week ago as of this writing)
    • CONS: install is not as easy as the others. It requires a PHAR file (not a real big issue), but it is not as easy as the others to get going in a Windows environment (again, not a huge problem, but it is more involved than the others)

In the end I decided to go with SimpleTest because its first few tutorials were very easy to get up and running, and it is very JUnit-like. The code base is also only running PHP 5.3.3, so it is on an older version of PHP, and the fact that SimpleTest (and Testify.php) were a little dated is not as big of an issue, and probably easier (as to not mix features of newer PHP versions) at this point in time.

At some point I should at least go through the Testify.php tutorials to see if it is a viable option.