Opencart 2.0 – PHP Coding (Function Arguments)

Continuing on with our PHP coding tutorials, in this article we will be discussing the topic of arguments. Arguments are used all over the spectrum of the OpenCart framework and are necessary for it’s very survival. Without the use of arguments there would be little to no way to pass certain pieces of data from one area to the next, at least with efficiency and speed.

What is a function argument?

Information can be passed to functions through arguments. An argument is just like a variable. Arguments can be looked at as ways to pass this information in a controlled manner where it can be accessed later in a different function or class.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

For the first example of an argument found in the Opencart 2.0 framework we will look to the following file: opencart-2.0.1.1\upload\system\library\image.php Within that file you will see the following function:

private function create($image) {
   $mime = $this->info['mime'];

   if ($mime == 'image/gif') {
      return imagecreatefromgif ($image);
   } elseif ($mime == 'image/png') {
      return imagecreatefrompng($image);
   } elseif ($mime == 'image/jpeg') {
      return imagecreatefromjpeg($image);
   }
}

First you will notice that this is a PRIVATE function which means it can only be accessed from within the current class file. If you run a search for

$watermark = $this->create($file);

You will see where the function create() is being called and where $file is being passed as the argument. By the time it hits create($image) , the prior $file variable is actually $image.  Arguments can have different names from function to function but must be in the same ORDER if multiple exist. Carrying on further if you look at the entire function where create() is called, which is here:

public function watermark($file, $position = 'bottomright') {
		$watermark = $this->create($file);

		$watermark_width = imagesx($watermark);
		$watermark_height = imagesy($watermark);

		switch($position) {
			case 'topleft':
				$watermark_pos_x = 0;
				$watermark_pos_y = 0;
				break;
			case 'topright':
				$watermark_pos_x = $this->info['width'] - $watermark_width;
				$watermark_pos_y = 0;
				break;
			case 'bottomleft':
				$watermark_pos_x = 0;
				$watermark_pos_y = $this->info['height'] - $watermark_height;
				break;
			case 'bottomright':
				$watermark_pos_x = $this->info['width'] - $watermark_width;
				$watermark_pos_y = $this->info['height'] - $watermark_height;
				break;
		}

..you will see the $file actually is yet another argument being carried over from

public function watermark($file, $position = 'bottomright') {

So you see, arguments can travel over many functions at a time.

Another good lesson in arguments:

If an argument is assigned a value within the parenthesis then it will take that value if nothing has been assigned to it being carried over (called) from a prior function. For example:

public function watermark($file, $position = 'bottomright') {

.. the bottomright string is assigned to $position variable/argument and will remain bottomright unless it has a pre-defined value being carried over from another function. So to make it more clear let’s say we call watermark() from a different file and it would call like this:

$position = 'bottomleft';
watermark($file, $position);

In this case the public function watermark($file, $position = ‘bottomright’) { would be bottom left because it was made that way. If the value would be blank or empty then it would assume a value of bottomright.

I hope this has given some insight into the basics of Arguments.

Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *