Posts Tagged ‘function’

How to Create PHP Static Method

About a couple of weeks ago, I covered the basics on how to Access PHP Class Variables and Methods. This article is different because it deals with only the creation of a static method. Before we continue, we will insure we know the difference between a non-static class method and a static method.

Non-Static Method Static Method
1 Requires creating a class object Can be called without creating a class object
2 Can call on other static methods and variables Can only call on static methods and function

The real world example of the differences between a non static method and static method can be an old full sized computer tower. In the static method point of view, assume you only want to use the computer tower as a foot rest when you sit at your desk. For this, you do not need to turn on the computer (create a class object). This does not require you to load you operating system (no instantiating of class variables required). But, you can still kick up your feet. However, because you did not turn on your computer, you will be unable to access non-static methods (can only call on static method methods).

In the non-static method point of view, if you want the operation system, you would need to turn on the computer (create class object). What this does is load your operating system (instantiate class variables) so you can finally login to your computer (method of the class). Additionally, with the computer on, we can still kick our feet on the tower (can call both static and non static function).

Now how do we create a function with static methods in PHP? When we write the static method, we must use the keyword static. This method we create will always return true and is created in the common class.

class common {
   public static function myStaticFunction()
   {
      return true;
   }
}

Now that we have created the static function, we can call on the static method with the following snippet of code. The first term “common” is the name of the class. This is followed by two colons (::) followed by the method name.

$value = common::myStaticFunction();

And there we have it! We have successfully created and used our static method. To see the official PHP article on static, go to http://www.php.net/manual/en/language.oop5.static.php.

Verify Email Address in PHP

Nearly every interactive web application requires at some point a user to register. So generally, there exists a registration page where site visitors input basic information like username, password, and email address. Aside from the username and password, what is the easiest way for a PHP web developer to verify an email address is at the very least properly formatted (whether this is an active account is a whole separate matter)?

The approach that come to mind first is to do a simple email string check:

  1. Make sure the email string contains at most one ‘at’ symbol (@)
  2. Make sure the email string contains at least one ‘period’ symbol (.) after the ‘at’ symbol (@)
  3. Make sure the email string contains at least one character before the at symbol (@)
  4. Make sure the email string contains at least one character between the ‘at’ symbol (@) and the ‘period’ symbol (.)
  5. Make sure the email string contains at least one character after the ‘period’ symbol (.)

For starters, this is no easy task. Note that the five suggested steps above is not even complete. Many might suggest a complex regex verification process. I have personally attempted to write a regex and even PHP based checker. During this process, I found a great PHP Email Address Validation function available under BSD License which, among others things, allows commercial use. (more…)

Accessing PHP Class Variables and Functions

So you have successfully created a PHP class and want access to the classes variables and functions. As a reminder to programming beginners, you can only access public variables and public functions. That is a function or variable that was declared with the public keyword or no keyword (as PHP functions and variables are by default public). Below is a quick refresher example where functionNumberOne and functionNumberTwo are public and functionNumberThree is private.

class Common {
   public $secondVariable = "2";
   private $thirdVariable = "3";

   function functionNumberOne() {
      echo "This is my first function";
   }

   public function functionNumberTwo() {
      echo "This is my second function";
   }

   private function functionNumberThree() {
      echo "This is my third function";
   }
}

If the class is declared in another a separate file, remember to first include the file before your first attempt to access the class. Generally, the best place to include files is at the very top of the file.

Assuming our Common class function above, our first step to accessing our PHP variables and functions is to create an object. In programming terms, this is known as initializing an object instance. To create the class instance:

$myInstance = new Common();

How was the previous code snippet generated? You can choose any name to replace the $myInstance variable (so long as it is not a keyword). Next, new is a PHP keyword that in this situation signifies the creation of a new object. Finally, Common() is constructed by using the class name (in this case Common) followed by an open parenthesis and close parenthesis ‘()‘. If you have a specified a constructor, make sure you include the correct number of parameters.

Finally, to access the public variables we use the $myInstance object followed by ‘->’ and the public variable or function name. When accessing the variable name, you do not need to start with $. Again, we have a code example below.

echo $myInstance->secondVariable;
$myInstance->functionNumberOne();
$myInstance->functionNumberTwo();

And there we have it. You now have access to public PHP functions and PHP variables. Note that the use of classes is a new feature to PHP 5.