Skip to main content

Posts

Showing posts with the label PHP

PHP Savers - PropertyBag !!!

The ubiquitous and the universal data structure in PHP is the array . It is an amalgamation of commonly used data structures - list, map etc. In the recent times, PHP has also adopted object orientation and introduced classes. The syntactic difference in the way a property of an array and object poses an inconvenience in the user code 1  specifically when there is a need to interact with code that is not open for change; legacy or not. JavaScript would allow you to access an object property either obj.propName or obj["propName"] . That does come in handy for sure. Besides, accessing the property by [] tags is the only way if the property name contains characters like hyphen: obj["prop-Name"] . At the user code level, it is fair to see an object as a bag of key-value pairs. Along the same lines, it is not wrong to expect the same in PHP between an object and an array; although there is a fundamental difference 2 . The expectation arises when ...

Overloading vs Variable Arguments !!!

In a statically typed (object oriented?) language, function overloading offers the facility of organizing your code into two or more functions with different types and/or number of arguments. This is highly useful when the functionality offered by the function can be invoked in different scenarios. For instance, let us consider the function(s) below: // C# code Dictionary<string, object> CreateResponse(string msg) { return CreateResponse(ex.Message, 0, false); } Dictionary<string, object> CreateResponse(string msg, int code) { return CreateResponse(ex.Message, code, false); } Dictionary<string, object> CreateResponse(string msg, bool success) { return CreateResponse(ex.Message, 0, success); } Dictionary<string, object> CreateResponse(Exception ex) { return CreateResponse(ex.Message, ex.HResult, false); } Dictionary<string, object> CreateResponse(string msg, int code, bool success) { var errorInfo = new Dictionary<string, object>(); erro...