Mar24


Static and non-static functions | Myths and Facts image

Functions are one of the most important tools for any programming language. Functions create the way to do some specific operations many times without writing the same code many and many times.

For Example:

<?php

$mary_maths = 10;
$mary_science = 6;

$ruby_maths = 5;
$ruby_science = 9;

function average(int $a,int $b)
{
return (($a + $b)/2);
}

?>


Marry got <?php echo $mary_maths ?> marks in Mathematics and <?php echo $mary_science ?> marks in Science. <br>
Marry's Average : <?php echo average($mary_maths,$mary_science); ?>
<br>
<br>
<br>
Ruby got <?php echo $ruby_maths ?> marks in Mathematics and <?php echo $ruby_science ?> marks in Science. <br>
Ruby's Average : <?php echo average($ruby_maths,$ruby_science) ; ?>

This Output will be:

Marry got 10 marks in Mathematics and 6 marks in Science. 
Marry's Average : 8


Ruby got 5 marks in Mathematics and 9 marks in Science.
Ruby's Average : 7

Here the same line of code can be used multiple times to produce the efficient result.

Functions in Object Oriented Programming

Functions in object oriented programming are classified into two main types:

  1. Static functions
  2. Non-static functions

These functions can be used, according to the need of the developer or the program. Moreover, There are several dilemmas on this between the developers about the efficiency and performance, one over the other. But don't worry, we will unveil the truth as we proceed

Static Functions

As its name suggests it is static means stationary. Stationary in sense of programming means that it cannot use other sources to fetch information, But it have a great advantage that it can be called without the initialization of the class object. 

Initialization:

A function can become a static function just by adding "static" before the "function" keyword.

For Example:

<?php
class Sample {

public static function foo()
{
echo "foo";
}
}


Sample::foo(); // statically calling; no object initialization

?>

In this case, it is clear that there is no initialization of class instance, which means we are not storing any useless data into the memory. Therefore, it increases the efficiency of the program.

But static function also has a very dark side, it could not fetch information from other sources.

For Example:

<?php
class Sample {
protected $hello = "foo";
public static function foo()
{
echo $this->hello;
}
}


Sample::foo(); // statically calling; no object initialization

In this case, we will get a Fatal Error:

Fatal error: Uncaught Error: Using $this when not in object context

As we have just said, it cannot use other sources to fetch information, which was the main feature of Object Oriented Programming.

Non-static Functions

Non-static means moving. In terms of programming, it means this function can fetch information from various sources in scope. Which makes it more friendly with the guidelines and objectives of Object Oriented Programming.

Initialization

<?php
class Sample {
protected $hello = "foo";
public function foo()
{
echo $this->hello;
}
}

$instance = new Sample; // defining the instance of the class
$instance->foo(); // calling the function from the instance i.e non-static calling

//output:
//foo

You can see here, "$this->hello" is used here to target the parent class i.e Sample's protected property "$hello". This is the power of non-static functions, it can fetch information throughout the class. But of course, it needs the class instance, we use memory, which in turn reduces the performance of the program.

Static v/s Non-static function

By doing some homework, we came get some real statistics regarding the performance of static and non-static functions,

  • PHP 5.2: static methods are ~10-15% faster.
  • PHP 5.3: non-static methods are ~15% faster.
  • PHP 5.4: static methods are ~10-15% faster.
  • PHP 5.5 and Above: static methods are ~20% faster. 

You can see here, generally, static functions are faster than non-static one. Obviously, the possible reason could be the initialization of class reduces the performance of non-static functions.

Do I always use static functions, because they are faster?

No.  Although static functions are faster but they have a very dark side, that they cannot fetch information from other sources in scope.

So, if your need is to get a universal information from the function you can use static functions.

For Example:

<?php

class Time
{


public static function now($Timezone = "UTC")
{
date_default_timezone_set($Timezone);

return date("Y-m-d h:i:s",time()) ;
}
}

echo Time::now();

//output
//2017-02-26 05:57:14

Here the static function has nothing to fetch from the external sources (Class). Therefore, the use of static functions in such type of cases is perfectly fine. 

Note:  A static function can also be called from the instance of the class object while non-static functions cannot be called statically, which is obviously an advantage of static functions.

On the other hand, the non-static functions are slower but it can fetch data from external sources which means it obeys the objectives of object oriented programming perfectly. 

For Example: 

<?php

class User
{

protected $users = ['Adnan','Rashid','Usman'];
public function countUsers()
{
return count($this->users);
}
}

$instance = new User;
echo $instance->countUsers();

//output
//3

Conclusion

If you want your function to use the Class data, you should use non-static functions. But if your function does not need to use any external data sources, it is best to use static functions.

Hoping that this article helps to understand the difference between static and non-static functions. If you have any query please comment us below.


|0 |60

Ads By Google - Turn it Off

Ads By Google - Turn it Off

Leave a Comment


gfhdg