Blogging from a CEO, Software Developer & Innovator!
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!
26 Jun
So has anyone read the Google C++ Programming Practices? Well I haven’t read it all, but it was recently discussed with myself and others about Google’s C++ Programming Practices. Very interesting… I do have to say.
The main point that was discussed the other day was Google’s C++ Copy Constructor practices. From what was discussed, Google’s method for handling copy constructors works, however really needs to be fixed. First off, their solution to the macro copy constructor prototypes is not technically valid. Secondly, they say they use boost (even though they say that they use only TR1 standards.) So, my question is why not just use the boost::noncopyable ? Come on Google! Get with it!
22 Jun
Hello Everyone!
Today, I am going to provide some updates and information on to what CyberGeek’s Software Solutions My Twitter Facebook application is, why we have developed it and what to expect in the next version update.
We here at CyberGeek’s Software Solutions have developed the My Twitter Facebook application as a experiment, testing ground for new marketing ideas and also to learn the Facebook Platform, FBML and how Facebook applications completely operate. The current My Twitter Facebook application was developed and released under the matter of 8 hours, where most of that was spent researching and reading documentation on the Facebook Platform and FBML.
Through the feedback and reviews from Users of the My Twitter Facebook Application, we have generated a list for version 2.0 of the My Twitter Facebook Application. Below is a list of features that will be included in the next version. Please feel free to comment on this post for feature list requests.
My Twitter – Facebook Application: Version 2.0 Feature List
We will be releasing version 2.0 late next week (June 28 – July 4).
Feel free to send requests for new features via comment here or posting on the My Twitter Facebook Application page.
We look forward to hearing from you!
12 Jun
Well, PHP has many quirks, but one boggles my mind the most. The quirk with using foreach( loops in PHP. So, here is an example of a simple foreach loop in PHP:
$g_array = array('key_one' => 'value_one', 'key_two' => 'value_two', 'key_three' => 'value_three'); foreach( $g_array as $key => &$value ) { echo $key . ' = ' . $value; }
However, that is simple. But have you ever noticed that after the iterations of the foreach loop have been completed and you are out of the foreach loop scope that you can still access the $value variable and which it will contain the last assigned value? In our case, if we did the following:
$g_array = array('key_one' => 'value_one', 'key_two' => 'value_two', 'key_three' => 'value_three'); foreach( $g_array as $key => &$value ) { echo $key . ' = ' . $value; } echo $value;
… the last echo statement of echo $value; will output the value_three. Kind of crazy eh?! Well, there are two solutions to this that you can do to fix this issue. One is that immediately after the foreach loop you can simply unset the $value variable or you can simply unset the value variable after each iteration. I personally like to place the:
unset($value);
outside of the foreach loop statement as this assures that we only make the unset call once and that now the $value variable will no longer exist and contain any data.
What you think about that? Anyways – off to do something productive!
11 Jun
Well as I had said & promised, during this time since we are currently in the staging of re-factoring and developing a new PHPWAF version, I had said I would be sharing various PHP development techniques and resources. So let it be, this is the first one of many to come.
So, have you ever wondered how to control the output of your PHP scripts or applications with ease? Well you can, by simply using the PHP output control library.
To start, lets create a simple script called example.php (or if you want to use a different name, go for it! Just make sure there are no conflicts with any other files we may be using.)
Alright, so within this script lets simply just perform some output with echo. Perhaps something like this will work to start off:
<?php
echo ‘Hello World! How are you today?’;
?>
So as you can see, we are simply just writing the text Hello World! How are you today? to the client’s (in this case, most likely your’s) web browser. Now what is happening is that the interpreter is simply just sending the text within the echo statement straight to the web browser with no internal handling, etc. (Well there is some, but no need to get down into the nitty gritties as it is, well we are not discussing that today).
Now, what we want to do is create another file named something like/similar to COutputHandler.php. Note, I used the prefix of a capital C as we will be placing a class within this file. It is a good idea to have a solid and very understandable file naming conventions as some projects may have thousands of functions or classes. If they are all within various files with inappropriate file names, it could take forever to locate the piece of source code you are looking for.
Within our new file – COutputHandler.php, we want to create a class that will be that can not be instantiated and acts like a static class (note: PHP does not currently provide exact support for static classes, or even visibility to the entire class only methods within it. However, we can mock this by using various techniques.)
The class declaration should look something like this:
final class COutputHandler {
}
Now I am sure some of you are wondering, what the bloody heck is final. Well, the final keyword simply just means that we can not extend (inherit) this class with any other classes. By attempting to inherit/extend this class PHP will throw a non-catchable fatal error.
Now, within this class (COutputHandler), we are going to need a few methods. These methods are going to be static (all of them except two). These methods are: initialize, ob_callback, _close_output_buffers, render, __construct, __destruct. Now, before you start and I am sure you are thinking to yourself, “Okay, I can create those.” remember this class is never going to be instantiated and will act as a static class. So, we are going to need to set the visibility of __construct and __destruct to private along with using the final keywords within the method signature. Also the __construct and __destruct methods are not static. Also, our _close_output_buffers method will take one boolean parameter which we will use to determine the type of internal PHP output buffer flushing, this method’s visibility is also private. So, name it appropriate. So, your class should now look something like this:
final class COutputHandler {
public static function initialize() {}
public static function ob_callback() {
}
public static function render() {
}
private static function _close_output_buffers($bool_flush_contents = TRUE) {
}
final private function __construct() { /* void */ }
final private function __destruct() { /* void */ }
}
So, is everything good so far? I hope so.
Before we start writing the method bodies’ code, we are going to need a couple private static member variables. One member variable will be a container used for holding the output buffer contents on the call back, along with an output integer value which will contain the output buffer level before we start anything.
So after updating your class, your class should now look similar to this:
final class COutputHandler {
private static $output = ”;
private static $m_int_ob_level;public static function initialize() {
}
public static function ob_callback() {
}
public static function render() {
}
private static function _close_output_buffers($bool_flush_contents = TRUE) {
}
final private function __construct() { /* void */ }
final private function __destruct() { /* void */ }
}
Alright, now lets start writing our class method bodies. We will start with the initialize() method. What we will want to do within this method is perform two critical things. One will be to start the output buffer control along with assigning a callback, and secondly we will want to set the level of the current output buffer as soon as we start it. So, to start output buffer control with PHP we will use the ob_start() method that is defined by PHP. So, first off lets take a look at this functions signature:
bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )
So by looking at this method signature, you should see and note that we can set a callback function along with other optional parameters. At the moment we are only worried about the callback parameter. Well what the heck is a callback function? Well instead of me explaining in great detail you can read about it here. So is it obvious yet what our callback function/method is going to be? It should be
We will be using our ob_callback() method as a callback.
So another thing we need to do within this class method is to get the current output buffer level after we call ob_start. So, we will use PHP’s output control function ob_get_level(). We will be assigning the returned integer to our m_int_ob_level static member variable.
… (typing away …) Now our initialize() method should look like this:
public static function initialize() {
/* Start output buffering and set our specific callback method */
ob_start(’COuputHandler::ob_callback’);/* Get the current output buffer level */
self::$m_int_ob_level = ob_get_level();
}
Now onto our second method – ob_callback(). Well before we continue, you should note and read about the specifications for the ob_start() callback function. (Looking for PHP docs, click here). However, just a quick overview and specific documentation for the output buffer callback, here it is:
An optional output_callback function may be specified. This function takes a string as a parameter and should return a string. The function will be called when the output buffer is flushed (sent) or cleaned (with ob_flush(), ob_clean() or similar function) or when the output buffer is flushed to the browser at the end of the request. When output_callback is called, it will receive the contents of the output buffer as its parameter and is expected to return a new output buffer as a result, which will be sent to the browser. If the output_callback is not a callable function, this function will return FALSE.
If the callback function has two parameters, the second parameter is filled with a bit-field consisting of PHP_OUTPUT_HANDLER_START, PHP_OUTPUT_HANDLER_CONT and PHP_OUTPUT_HANDLER_END.
If output_callback returns FALSE original input is sent to the browser.
The output_callback parameter may be bypassed by passing a NULL value.
So you will see and have read that our output buffer callback method – ob_callback(), must accept/take at least one parameter as a string and return the same or new value which will be sent to the browser when flushed.
So, lets code our ob_callback() method. The string parameter passed should be appended to our member variable output. Our ob_callback method will return the same value as passed to it. Our method should look something like this:
public static function ob_callback($internal_buffer) {
self::$output .= $internal_buffer;return $internal_buffer;
}
The next method we will code is going to be our private static _close_output_buffers(). Within this method, we want to do a couple of things. We will first want to get the most current output buffer level with ob_get_level(), check to see if the initial output buffer level stored in our member variable m_int_ob_level is less then the most current. Now, if the current output buffer level is greater then the initial, we will want to do a couple of things. If it is not, we want to simply just return false. So, what are we going to do if the current output buffer level is greater then the initial? Well, first we are going to generate a dynamic function callĀ back to either ob_end_flush or ob_end_clean depending on the boolean parameter value. If the boolean parameter value is true, we are flushing the PHP output buffer contents so we will want to use ob_end_flush else we will be using ob_end_clean as the dynamic function callback. So this can be acheived with a simple ternary operator assignment, like this:
$ob_end_callback = ( TRUE == $bool_flush_contents ) ? ‘ob_end_flush’ : ‘ob_end_clean’;
Some of you may not know what a ternary operator is. Well, it is simply an inline if statement. First we have the condition to check. If it is true, we will assign the value that is after the ? but before the :. Else, if the condition is false we will assign the value after the colon (:).
We could have also wrote the same code with multiple lines by using an if-else statement. Using the if-else statement, it would have looked something similar to this:
if( TRUE == $bool_flush_contents ) {
$ob_end_callback = ‘ob_end_flush’;
}
else {
$ob_end_callback = ‘ob_end_clean’;
}
Now after we have generated a dynamic function callback assigned to the ob_end_callback variable, we want to perform a while-loop until the most current (we will use ob_get_level()) is greater then the initial output buffer level which is stored in our m_int_ob_level member variable. Within this while loop, we simply just want to call the dynamic function callback. So, our while loop should look something like this:
while( self::$m_int_ob_level < ob_get_level() ) {
$ob_end_callback();
}ob_get_clean();
The only remaining code required for this method is to simply update our member variable containing the output buffer level by performing a simple assignment with the return of the ob_get_level function.
Alright, one last thing for this method! We want to return a boolean value depending on if output buffers were closed or not. So, we want to simply place a return TRUE and a return FALSE in the appropriate locations. Our final _close_output_buffers method should look like this:
private static function _close_output_buffers($bool_flush_contents = TRUE) {
$int_ob_level = ob_get_level();
if( self::$m_int_ob_level <= $int_ob_level ) {
$ob_end_callback = ( TRUE == $bool_flush_contents ) ? ‘ob_end_flush’ : ‘ob_end_clean’;while( self::$m_int_ob_level < ob_get_level() ) {
$ob_end_callback();
}ob_get_clean();
self::$m_int_ob_level = ob_get_level();
return TRUE;
}return FALSE;
}
Now for our final method, the render method. This is where we will do any final rendering/parsing of the output data stored in our output member variable, along with echo’ing it. So, for this demonstration purpose, we will want to simple just have a list of tags that can be replaced from our original data with new data. So to start, we MUST close our output buffers before we do any final rendering. So, we we will call out _close_output_buffers. After words, lets perform a str_replace() on the output data. But first, lets some up with a couple of simple tags/tokens that will be replaced from our original data with new data. So, I am going to use the following:
So lets use a str_replace() function specifying the search and replace parameters as arrays. So, this should look like:
self::$output = str_replace(array(’{TAG_ONE}’, ‘{SOME_OTHER_TAG}’), array(’HELLO WORLD!’, ‘OKAY! YAY! I HAVE BEEN RENDERED!’), self::$output);
Now, after performing our string replace call we will simply just want to echo our output variable. This render() method should now look like this:
public static function render() {
self::_close_output_buffers(TRUE);self::$output = str_replace(
array(
‘{TAG_ONE}’, ‘{SOME_OTHER_TAG}’
),
array(
‘HELLO WORLD!’, ‘OKAY! YAY! I HAVE BEEN RENDERED’,
),
self::$output
);echo self::$output;
}
Note that the string replace tokens I have used were only for this demonstration. This can be changed dynamically so that you can easily specify search tokens and replacement data. However, I will demonstrate this at a later date.
So our class is complete! The COutputHandler class should now look like this:
final class COutputHandler {
private static $output = ”;
private static $m_int_ob_level;public static function initialize() {
ob_start(’COuputHandler::ob_callback’);self::$m_int_ob_level = ob_get_level();
}public static function ob_callback($internal_buffer) {
self::$output .= $internal_buffer;return $internal_buffer;
}public static function render() {
self::_close_output_buffers(TRUE);self::$output = str_replace(
array(
‘{TAG_ONE}’, ‘{SOME_OTHER_TAG}’
),
array(
‘HELLO WORLD!’, ‘OKAY! YAY! I HAVE BEEN RENDERED’,
),
self::$output
);echo self::$output;
}private static function _close_output_buffers($bool_flush_contents = TRUE) {
$int_ob_level = ob_get_level();
if( self::$m_int_ob_level <= $int_ob_level ) {
$ob_end_callback = ( TRUE == $bool_flush_contents ) ? ‘ob_end_flush’ : ‘ob_end_clean’;while( self::$m_int_ob_level < ob_get_level() ) {
$ob_end_callback();
}ob_get_clean();
self::$m_int_ob_level = ob_get_level();
return TRUE;
}return FALSE;
}final private function __construct() { /* void */ }
final private function __destruct() { /* void */ }
}
Alright, so before we test this! Lets do a couple of changes and additions to our example.php file. First off, before we call the echo statement in the example.php, lets perform the require() call on our COuputHandler class file. After including/requiring this file, lets perform the initialize() call. This should look like:
require_once(’COutputHandler.php’);
COutputHandler::initialize();
Now in our echo statement, lets place our search tokens so that we can demonstrate how this output control works. My echo statement now looks like:
echo ‘{TAG_ONE} {SOME_OTHER_TAG}’;
After the echo statement and directly at the end of the script execution, we should be calling the COutputHandler::render method. So, lets do so. Now,your entire example.php should look like this:
<?php
require_once(’COutputHandler.php’);
COutputHandler::initialize();echo ‘{TAG_ONE} {SOME_OTHER_TAG}’;
COutputHandler::render();
?>
Yay! It’s time to fly! (not literally). So, test it out. It should be working like charm now for you! If you have any problems feel free to comment/ask for help via comments. I have also attached my entire solution for this demonstration in a .zip file. I have also placed comments within the solution to guide you / explain anything major.
Download Output Control Example Here
Thank you and if you like this post, please let me know so I can write more! Share it with your friends also!
Note: This example requires at least PHP version 5.2.0 or greater!
9 Jun
Well over the course of the next few weeks to month or so, I will be leading the development of a new PHPWAF framework. During this time, I will be posting various code snippets, library packages, function helpers and more which will allow and assist all of you PHP developers out there that may need some assistance or want to expand your knowledge in the world of PHP. Some of these tutorials, walk throughs and code snippets will also be placed at Web Help and Resources.
Oh! By the way, CyberGeek’s Software Solutions has just launched and released Web Help and Resources which we hope and aim to be the one centralized website that any one can find help, resources and learn about all things web related.
Anyways, I am off for now to start the kick off stages of the new PHPWAF package. I will keep you posted!
6 Jun
Within the next week CyberGeek’s Software Solutions will be launching a new web blog named – Web Help and Resources. Web Help and Resources will be an online resource center of articles and tutorials for all things web releated.
Web Help and Resource’s mission and goal is to provide the people around the world with a centralized location where they can receive help and resources pretaining to web related topics. Some of the categories that will be used are (note this is not all of them, just a few) Business, W3C Standards, Server Side Programming (ASP.NET, C#, PHP, Ruby), XHTML and CSS, Design, Search Engine Optimization and many many more! If you have any suggestions related to more categories, please feel free to contact me at aaron.mcgowan@cybergeeksoftware.com.
We are working diligently to finish our final touches and get ready to launch! We look forward to seeing you in the near future.
I will keep you informed on our progress and when we launch!
5 Jun
Hi everyone!
A usefull piece of C++ source code for you to convert std::string to std::wstring. Please note this was not original it was shared from another fellow developer. However, it has been modified for appropriate variable naming conventions. Because seriously, who likes variables named x, y, z (unless you are dealing with specific cordinates). How about temp? Like seriously what the heck does temp mean?
So, here it is …
#include <string>
std::wstring string_to_wstring(const std::string& _string) {
std::wstring widestring(_string.length(), L”);
std::copy(_string.begin(), _string.end(), widestring.begin());
return widestring;
}
We hope that someone finds this useful! Anyways, back to documenting … Boo!