Blogging from a CEO, Software Developer & Innovator!
23 Sep
For any of the hardcore and or amature PHP developers out there reading this, listen up! I am sure some of you or most of you know the Singleton Object Oriented Design pattern, and if you don’t well … I will briefly explain it, but you should Google it also
.
The singleton object oriented design pattern is used to restrict/limit the number of instances to a single (one) object instance of a specific type (meaning class). Further reading can be done at Wikipedia.
The singleton design pattern in PHP is implemented constantly. Or should I say, it has been partially implemented. There are numerous ways to partially implement the Singleton design pattern in PHP. The majority of them are basic and simply constructed with a private constructor method and a public static instance or get_instance method which usually returns the single (or so the developers want it to be) instance of that class. This instance is stored usually in a private static member variable (property) of the class. So, it usually looks something like this:
/** * @class: MySingleton */ class MySingleton { /* Member variables */ private static $ms_instance = NULL; public static function &instance() { if( NULL === self::$ms_instance ) { /* This could also be written as: self::$ms_instance = new MySingleton(); */ self::$ms_instance = new self(); } return self::$ms_instance; } private function __construct() { /* void */ } public function __toString() { return get_class($this); } /* Other methods of this class ... */ } /* end class MySingleton */
The example above demonstrates how the majority of PHP developers implement the Singleton design pattern. However, this is not fully correct. The reason it is not correct is because of the following:
$obj = &MySingleton::instance(); echo $obj; $obj_clone = clone $obj; echo $obj . ' - ' . $obj_clone; $obj_clone2 = clone MySingleton::instance(); echo $obj . ' - ' . $obj_clone . ' - ' . $obj_clone2;
The multiple instances can be demonstrated with the modified “MySingleton” class below:
/** * @class: MySingleton */ class MySingleton { /* Member variables */ private static $ms_instance = NULL; private static $ms_count = 0; public static function &instance() { if( NULL === self::$ms_instance ) { /* This could also be written as: self::$ms_instance = new MySingleton(); */ self::$ms_instance = new self(); } return self::$ms_instance; } private function __construct() { ++self::$ms_count; } public function __clone() { ++self::$ms_count; } public function __toString() { return get_class($this) . ' - Instance Count: ' . self::$ms_count; } /* Other methods of this class ... */ } /* end class MySingleton */
Below is an example of how to implement a Singleton Object Oriented Design pattern in PHP 5.2.x. Note that you can use the example below also in PHP 5.3 however, PHP 5.3 has late static binding features implemented which will allow you to utilize a more efficient implementation of the Singleton OOD which will allow easier code base maintenance.
/** * @class: MySingleton */ class MySingleton { /* Member variables */ private static $ms_instance = NULL; /** * @method: instance * @description: Singleton method instance used to construct the * singleton instance of MySingleton class along with accessing it. * @access: public static * @param: void * @return: [MySingleton] Returns the singleton instance of this object. */ public static function &instance() { if( NULL === self::$ms_instance ) { self::$ms_instance = new self(); } return self::$ms_instance; } /** * @method: __construct * @description: Ctor. * @access: private * @param: void * @return: void */ final private function __construct() { /* void */} /** * @method: __clone * @description: Copy Ctor. PHP Magical Method. Invoked when object is copied and or cloned. * @access: private * @param: void * @return: void */ final private function __clone() { /* void */ } /** * @method: __toString * @description: PHP Magical Method. Invoked when object instance is used as a string. * @access: public * @param: void * @return: [string] Returns the name of this class. This will be this class and or any derived types. */ public function __toString() { return get_class($this); } } /* end class MySingleton */
Here are examples for testing this Singleton class:
$obj = &MySingleton::instance(); echo $obj; /** * The example below will raise a FATAL ERROR halting the script execution. * * $obj_clone = clone $obj; */ /** * The example below will also raise a FATAL ERROR halting the script execution. * * $obj_clone2 = clone MySingleton::instance(); */
Anyways, I hope this helps and clears up some confusion for any of you on how to properly implement the Singleton OOD pattern into your applications. Please comment and let me know what you think!
I am off for now! Everyone have a good evening.
27 Aug
Hows everyone? Well that’s good. So, as you may know that I am currently on vacation … and what a surprise! I am still constantly working and coding. Oh well, I love it
So that is a bonus I guess? But, just a general note; my vacation has been a blast so far!
Now onto more important things, I am releasing a newly updated and stripped database library that we constantly use here at CyberGeek’s Software Solutions. This is the initial public release so please feel free to critique, examine and make suggestions. However, unfortunately there is no public documentation and probably won’t be to be honest. The reason this is, is well … I am just to lazy at the moment to generate public documentation for this package and since all our employees (well at the least the ones who use the package) know the structure and also the usage of it very well I see no reason why to create documentation. However, there is documentation within the various files so you may want to look at that. Now, onto real things … You can download this package here: [ CGSS-DatabaseLib-1.0.0 ].
Currently the only supported DBMS driver is MySQL, however we will be releasing a MS SQL and MySQLi drivers here over the next couple of weeks. There is also a built in caching system to automatically cache queries (if you wish). The current cache driver is only for file based caching. Note though that we will be releasing a mem_cache driver soon also.
Anyways, everyone take care and I will keep you all updated on my extremely boring life.
20 Aug
“The fastest growing crime in Canada is Identity Theft” is what a RBC Insurance Call Center Technician told me this morning. Now, I am not going to argue about that as I honestly do not know if it is or isn’t. However, I had a nice little chuckle today over some information he was trying to sell me on today about their services.
Now this was a very interesting conversation that I had with this guy right up until when I do believe I ticked him off because he started to raise his voice and get very very defensive. So this man (I honestly do not know his name, as I honestly don’t know if he even said it – which if he did not, I do believe he was suppose to).
So like every other telemarketing phone call that the majority of people receive it started off the same with the caller stating where he is from/who he is working for, what the call is about and that the call will be recorded for Quality Assurance purposes. As most call receivers, I sat there listening to the same old routine (I would like to see a company mix it up a bit, keep the recipient of the call a bit more interested – for once I was not a jerk and did not hang up). The caller was going on about how identity theft in Canada is the fastest growing crime in Canada, and how RBC Insurance’s services can protect all my information. From what the caller had said, they would insure and protect my name, SIN, phone number, credit cards, bank account information, address, etc. etc. etc. The list went on and on. Now sure it all sounds good nice and dandy to the average Canadian. However, here is when things got hazy.
“We do a complete scan of the entire Internet every single day for all your information and any fraudulent uses of it.”, said the caller.
I immediately perked up and asked him to repeat what he said (either way, I was going to refuse the services as I find that they are ridiculous to begin with and IF I wanted them, I would simply just walk into the freaking Bank and get them myself). So, as I had requested the caller repeated what he said and he went a little more in depth about what they scan the internet for in regards to my personal information. And he simply repeated his big long list of items. I immediately told him that he was full of crap and it is not possible. This is immediately when he raised his voice and got very defensive and said “How do you figure?”. I simply responded by saying, “I am Project Manager in the IT industry, Senior Software Developer and I specialize in web technologies. Scanning the “entire” Internet every single day for a single person’s information would simply overload and crash the your entire network infrastructure.” So, the caller once again in a loud and defensive response stated “We have a state of the art network and it is easily done”. I literally laughed out loud and told him he was full of horse crap. Not trying to be rude I simply said “it is not possible – the Internet in a whole is larger then what you think it is, secondly if my information was stored in a database that is placed on a web server and my information was within a secure area of a web application and or other software you would then get charged for illegal cyber activities and RBC would not be in business for to much longer.” The caller was once again very defensive about his response”, however he was very quickly lost for words to response to my last statement. I quickly said that “I was not interested and I was not right from the start of your call but to amuse you I did not hang up. Now please remove my phone number from your call list and if I receive another call about these ridiculous services that are trying to be sold to me by a very uninformed person like yourself, I will simply be moving my entire business banking and all my family’s banking to another bank. So in closing, have a nice day and DO NOT CALL ME BACK!“. And then I simply hung up the phone and had a nice little chuckle about the entire little incident.
So after all of this, I simply did a Google search for the number that they phoned from (Gotta love Google eh – it’s like my best friend and should be yours too
). Google pulled up numerous results. I clicked on the first result to the 800notes.com website. Here is the link: http://800notes.com/Phone.aspx/1-877-343-3683. Take a little read.
I also called the 1-800 number back and found out that the actual company is BRI. From their nice little automatic voice message, they said that they handle the marketing of numerous insurance and financial institutions.
I see that the BRI is not following legal standards by calling numerous people on the Do Not Call List. Did I mention, I am on it to?
So in closing if the number 1-877-343-3683 calls you, you are most likely getting called from a machine to begin with. Which then once you pick up, you are routed to a very uninformed call center technician. So watch out!
On a few side notes and in my own opinion, the issues in regards to Identity theft, the rising numbers are most likely caused by idiots who work in call centers and the very unintelligent recipients who answer the call. Why do I say it both your faults? Well its simple. They ask, you give!
16 Aug
So everyone, as I have been saying that CyberGeek’s Software Solutions is coming out with a new product I just always wanted to let you know of a couple other small notes on it and or items that are being developed for it and to develop new components for this new product. WOW! Did I just say that? Ssssh! Don’t tell anyone.
Anywho, we have also been in the midst of developing a custom IDE for our new product. In saying this, we are developing the IDE in C#. So, saying this – since C# is limited but also extremely powerful, we have had to think out of the box to develop the source code editor for our product. The source code editor that we are developing (well actually, I am developing …) is based on the ICSharpCode’s TextEditor control. However, we are strictly rewriting and modifying many parts of it.
This new source code editor control is going to include numerous built in features. Some of these features are but not limited to the following:
Those are just some of the features that will be included by default. Anyways, this source code editor control will be available as open source through my blog (here…). However, we are not ready for releasing it to the public as it is still being developed at the moment. I will keep everyone updated on this. I look forward to hearing your ideas and thoughts.
14 Aug
So, as I am sure everyone who reads my blog has noticed the lack of posts lately. I do apologize, however it has been kind of crazy. A lot has been going on. So, let me give everything a bit of an update.
First, I am just about done school! Well, this semester at least. If you don’t know, I am a student in the Computer Programmer Analyst course at Fanshawe College here in London. It is definitely an extremely interesting course for those who are not already not programming as a career. However, nothing against the course itself and or the professors but there are some downfalls. I tend to get extremely extremely bored. Anywho, enough of that jazz.
So, after my final exam next week on Friday the 21st of the August, I will be taking a 2 week vacation/trip to western Canada. I will be visiting Calgary, Edmonton and Lloydminster. The plan at the moment is to stay with a good friend in Calgary for a week and then head up to Lloydminster where I will be visiting my grandparent and other close friends. I will be arriving back home on the 6th of September however I plan on spending at least half a day in Edmonton to go and visit the ol’ mighty West Edmonton Mall (it is honestly not that exciting… At least in my opinion.).
There are some new and exciting things that we are doing here at CyberGeek’s Software Solutions and we have dedicated enormous amounts of time to R&D for these soon to be announced products. This is basically where all my time has been going, and I know it will pay off. I will definitely keep you guys posted and hope to announce these products to the public here soon within the next month.
Anyways, I am off for now. However, I promise to start posting some more source code and general blog posts.
Have a great day everyone!
6 Aug
Hi Everyone,
So over the past couple of days I have personally been working on a Windows Application written in C#. The application utilized a Tab Control for allowing multiple items to be opened/edited at the same time in various tab pages. One of the requirements required for the tab control was to be able to utilize a right click context menu when clicking on individual tabs. Well… Since C# has no distinct ability to determine where the right clicked occurred in respect to what tab, I was required to write a little helper function. The helper function simply allows to determine what tab was clicked and returns that tab index. Alright, so enough of my jibber jabber. Here is the method that was coded and the documentation for it. Do I need to explain any further?
/** * @method: FindTabIndexForPoint * @description: Handles the retrieving of a tab index for a specified point. * @access: public * @param: [System.Windows.Forms.TabControl] Contains the tab control to use for searching. * @param: [Point] Contains the point object that we are looking for. * @return: [integer] Returns the tab index if found else returns -1; */ public static int FindTabIndexForPoint(ref System.Windows.Forms.TabControl tab_ctrl, Point pt) { for( int i = 0; i < tab_ctrl.TabCount; ++i ) { if( tab_ctrl.GetTabRect(i).Contains(pt) ) { return i; } } return -1; }
Anyways, I have posted this as this is a great and useful little snippet of code! Have fun and happy coding.
2 Aug
Well hello everyone! It has been a while since I posted and I do apologize. Anyways, just to provide some news about a few things. First off…
I am unfortunately sorry to announce that I have discontinued Facebook My Twitter application development for version 2.0 and I have also “deleted/removed” the application from Facebook. Unfortunately I no longer have time to continue development and or support for it. My sincere apologies.
Another big topic that will soon hit the news is the release and announcement of a new product that CyberGeek’s Software Solutions is currently in the middle of developing.
Anyways, I am off for now. I got work to do …
1 Jul
Good Morning Everyone!
First off, HAPPY CANADA DAY!
Now, another great topic to mention is that PHP has just released PHP version 5.3.0. !
28 Jun
Well just recently I have discussed the topic of static class implementation with PHP. Well, as we are hard at work here on a new product (soon to be announced), I have developed an abstract static class which can be used simply for implementing of static classes. Here I have attached the abstract static class file so that you can download and easily use the same method as Professional PHP developers.
Please note that since we are in the middle of working on a new product here at CyberGeek’s Software Solutions, our product is only supported by PHP 5.3.x. So, this library class also only supported with PHP version 5.3.x. However, you can simply modify the source to make it work with previous version (Remove the namespace implementation).
Alright, so how do you use it? Well it is simple. Simply include the library class file with a require or include statement (recommended: require_once or include_once). Now, when creating your class, simply define your class like this:
final class MyStaticClass extends \PATTERNS\STATIC_CLASS\StaticClass { }
So what does this all mean? Well simply the \PATTERNS\STATIC_CLASS\ is the namespace from the global namespace which the StaticClass class is defined. Therefore, you want to simply just extend this class. Make sure you usefully qualified namespace implmentation as it is the most appropriate and proper PHP programming standards.
Download the PHP source code here.
Anyways, feel free to ask any questions and or concerns that you may have! Happy Coding!
26 Jun
How is everyone today? Anywho, today with this post I am going to discuss PHP’s Object Oriented Programming & Design techniques for Static Classes.
Well I am sure that if you have came across this post and you have read this far, you are looking to figure out how to create static classes in PHP. Well guess what? You can not! However, PHP does provide the necessary class method modifiers to mock this type of behaviour.
Unlike other languages like Java, with PHP we have to do a little bit more work to accomplish the same effect. Okay, so with this article I am assuming the readers know how to create classes and terms in object oriented design & programming.
Steps for creating a class in PHP with static behaviour:
First we want to create an empty class definition.
class MyStaticClass { }
Secondly, we do not want to instantiate this class ever! So, what we want to do is create the constructor and destructor for this class with private visibility modifiers. We also want to include the final keyword on this method declarations. Reason being, we should never be able to instantiate this class and or derived classes should never be able to be instantiated. So, our class should now look something like this:
class MyStaticClass { final private function __construct() { /* void */ } final private function __destruct() { /* void */ } }
And now, we have our first Static class definition in PHP. Now, what we want to do is make sure that all other methods and member variables within this class all have static modifiers.
We also want to remember that any derived classes do not need to have a constructor or destruct method definition. Reason being is that the final keyword used in our static class’s constructor and destructor method declaration simply means that this is the only method that can be the same signature within any derived classes.
Anyways, I must get back to work…
Hope you all have a great day and if you have any questions or concerns please feel free to comment!