News Feed
Jobs Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Johannes Schluter's Blog:
Class posing and overloading the "new" operator
January 07, 2010 @ 10:24:16

In this recent post to his blog Johannes Schluter talks about a method he's suggested for testing objects in unit tests - overriding the "new" operator to replace specific classes with mocks.

Two years ago at some conference I had a conversation with Sebastian about the need for a way to overload the new operator in PHP so, probably, bad designed code can be tested more easily by replacing specific classes with mocks. [...] Sebastian then pushed the code as part of a new test_helpers extension with some documentation to github and I fixed some bugs in it. The aim of the extension is to collect functionality which might be beneficial for phpUnit and other test scenarios but which should never reach a production environment.

He includes some sample code to show it in action - defining the mock class, using the set_new_overload function to define it as what should be called when the "new" operator is used and a dump of the result.

1 comment voice your opinion now!
overload new operator mock unittest


blog comments powered by Disqus

Similar Posts

MaltBlue.com: Why Testing Makes a Project Successful and You Canít Afford to Deny It

Zend Developer Zone: Episode 035: "Unit Testing in Zend Framework 1.8"

Greg Beaver's Blog: Pyrus, PEAR2 and web code coverage report for phpt-based tests

Sameer Borate's Blog: Test dependencies in PHPUnit 3.4

Community News: New York PHP Conference & Expo 2006


Community Events











Don't see your event here?
Let us know!


language component hack facebook hhvm security symfony2 composer example unittest framework release install introduction podcast application database performance package project

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework