An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ variable ’

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.

Creating a Class in PHP

by Victor | September 1, 2008 in PHP | 1 Comment

The object oriented programming concept is great for not only organizing code, but also easing readability. Creating a class in PHP is easy and we will go through the basics. For starters, creating a PHP class is as easy as:

class myFirstClass {
   // CLASS CONTENT GOES HERE
}

The next step is to add variables and functions to the class. One of the first functions every class has is a constructor. A constructor is a function that is run when the class object is created. To create a constructor function for your class, a default constructor function name by “__construct()” is used. See the example below

class myFirstClass {
   public function __construct() {
      // CONSTRUCTOR CONTENT HERE
   }
}

Next, you can add variables to your class. Variables can be defined as public or private. Private variables can be accessed only from functions belonging to the class. Public variables on the other hand can be accessed by functions belonging to the class along with functions outside the class. Once we have defined our variables, we can initialize them in our constructor. Below is an expanded example of a public and private variable.

class myFirstClass {
   private $myPrivateVariable;
   public $myPublicVariable;

   public function __construct() {
      $this->myPrivateVariable = "Private Message";
      $this->myPublicVariable = "Public Message";
   }
}

Lastly, we can add functions to our class. Because the $myPrivateVariable is private, we cannot assign it without a custom function. Therefore, in our next example, we will create a function that assigns a string value to the $myPrivateVariable. This function should be public since we want to be able to access it from outside of the class.

class myFirstClass {
   private $myPrivateVariable;
   public $myPublicVariable;

   public function __construct() {
      $this->myPrivateVariable = "Private Message";
      $this->myPublicVariable = "Public Message";
   }

   public function assignMyPrivateVariable($message) {
      $this->myPrivateVariable = $message;
   }
}

Now, we have completed a very basic PHP class. Follow the same steps to create more variables and more functions to make the class more powerful and useful.

One of my favorite and most heavily used PHP common functions retrieves variables from the query string. I found that without this function, I was inefficiently checking if the variable existed, and if not, I was then was manually setting a default value. The function takes in two string variables that handle the above problem. $urlStringName is the name of the variable as stated in the query string and $returnIfNotSet is the string that will be returned if the $urlStringName is not found. Check out the code snippet below:

function getUrlStringValue($urlStringName, $returnIfNotSet) {
  if(isset($_GET[$urlStringName]) && $_GET[$urlStringName] != "")
    return $_GET[$urlStringName];
  else
    return $returnIfNotSet;
}

Let’s run through an example of how this function works. Assume we have the following url:

http://www.victorchen.info/index.php?firstName=Victor&lastName=Chen

If I am looking to find the string value of the first and last name, but return john or doe if the query variable firstName or lastName is not found, respectively. I would write the following:

$firstName = getUrlStringValue("firstName", "john");
$lastName = getUrlStringValue("lastName", "doe");

In this example, we would expect to store $firstName to be “Victor” and $lastName to be “Chen”.

However, if the url arrives as (missing the lastName query string variable):

http://www.victorchen.info/index.php?firstName=Victor

We can expect to see $firstName as “Victor” and $lastName as “Doe”.