Using a Message Object in PHP

in #php5 years ago

I like talking about programming theories and code. I probably should mention that I do not personally know anyone who likes my coding style.

I like object-oriented design as envisioned by Alan Kays. In this vision one uses a group of programs called objects to encapsulate resource. The objects communicate with eachother via messages.

I am not a fan of the class structure used in C++ and Java. I prefer the functional approach.

My ideal system involves a backend database that communicates with a variety of channels. The channels would send a command to the back end server. The server would perform an operation and send a message back to the user.

For enterprise systems, I would create a complex message object that would do fancy things such as texting the admin if things went wrong.

For the moment, I am writing extremely light PHP sites; so, I created a lightweight pseudo object.

This code viewer shows a page that calls the message object. If you load the code viewer; you will see a line that reads "require('msg.php');" Clicking on that line expands and contracts the file that shows the msg.php object.

temp.png

The msg object that I used for PHP programs is extremely lightweight. It has four primary functions. The functions are:

  • msgNote() records that an action was successful
  • msgError() records that an action failed and sets an error flag.
  • msgOkay() returns the message flag.
  • msgHTML() will print the messages for the users.

In the ideal world, there should be one and only one message object for a system. The message object should be global.

PHP does not allow for global objects. So, I decided to do the next best thing and stuff the message data in the $_GLOBALS[] variable.

Yes, I know, the technorati warns against directly accessing the $_GLOBALS[] variable.

I need to repeat. I am the only person who I have personally met who likes the idea of building a system around a message object. I developed two enterprise systems that featured a message object. Both systems were extremely robust and easy to administer. (Both companies failed because of corporate mismanagement. They both had a very robust computer system when they failed.)

The messages help with debugging. My sample code has a function called msgComment(). This creates a comment for a programmer. One can design the system to send messages to different people. For example, a system could send messages to security if it detects a DDOS or SQL Injection attack.

A robust message system can turn services on or off if user load is high. I don't have that feature active in my PHP msg Object.

On enterprise systems, I would incorporate the messages with a help system that would provide documentation for each of the messages.

Message Object v Error Handling

The message object is similar to the exception object used in a variety of programming languages.

A message object is more powerful than the exception object because it allows people to track both successful operations and exceptions. IMHO, try {} catch {} blocks are a half assed solution as the system should be tracking both successful operations and errors.

I like the idea of throwing and catching objects. It would be fun to build a system that simply involved computers tossing messages back and forth between users.

Conclusion

I tend to optimize the messaging object for each system that I work on. This is rarely a problem as messages can encapsulate each other. One can send messages between different systems as delimited strings. They can be in JSON, XML, CSV or other format.

My next post will be about my solution to the mess that Zend created with the PDO object.

Sort:  

Congratulations @yintercept! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You distributed more than 5000 upvotes. Your next target is to reach 6000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

The Hive Gamification Proposal #2