Difference between Interface and abstract class – PHP Tutorials

Many of us normally end up with confusion about interface and abstract class, so in this post i am trying to clear it out, and highlighting the usuages.

What is Interface?

Interface is just a interface as the name implies, it just a definition of objects and methods with no body  on it. So its a collection of objects and methods that can be used by other class to perform common function. So its a just a place to specify objects and methods but does not explain you, how it is used or defined.
Its a practice to make clear rules on class.

So in summary, interface doesn’t do any thing, its just a pattern to be follow.

Class can inherit more than one interface.

What is Abstract class?

Abstract class is little more than interface classes, it not only defines the objects / methods  but also explain, what it suppose to do as default.  It may / may not define all the methods so it can leave some of the method to be implement by those who inherits it. Class who inherits it can add more methods / objects, those are not provided by the abstract class unlikly to interface.

Objects of an abstract and interface class cannot be created.

Private methods cannot be define in Abstract class as its cant be used by classes using abstract.

What is the Difference between Abstract Class and Interface

  1. Can inherit only one abstract class but can inherit more than on interface.
  2. Abstract provides complete code but interface is just a pattern to be follow.
  3. Abstract class is faster as it has complete code while interface is just a pattern so more classes are loaded.
  4. If new method is added to abstract class, all class that inheriting might work but if we add method in interface we have to define method in all inherited classes.
  5. Class using abstract classes may / may not follow everything from abstract class and call add more  but interface must be followed.
  6. Class can overwrite the method of abstract class but interface we just follow the same.
  7. Completely non related classes can follow same interface but in abstract this may not be possible.

Example of Abstract Class:

abstract class Member {
    public $fname;
    public $lname;
    public $address;
    public $phone;

    public function addData($fname,$lname,$address,$phone) {
    }

    protected function getName() {
    return $this->fname.' '.$this->lname;
   }
}

Class using Abstract class:

class Employee extends Member {

    public $emp_name;
    public $emp_address;
    public $emp_phone;

    public funtion getFullName() {
        $this->name = $this->$emp_fname;
        $this->lname = $this->$mp_lname;
        echo $this->getName();
}                 
          public function someFunction() {        };
}

In above example, class inheritting abstract class Member 
is only using one method of the class. And also has some extra class on it. 

Example if Interface:

Interface  Member {

    public function addData($fname,$lname,$address,$phone);

}
class Employee implements Member {

    public $emp_name;
    public $emp_address;
    public $emp_phone;

    public funtion addData($fname,$lname,$address,$phone) {
        // code

        }                 }
Advertisements

Is MySQL query Caching really good for performance optimization ?

MySQL query cache is one of the option on MySQL server optimization.  But question here is, is it really good to do MySQL query Caching?

To understand this we have to know what really happens during MySQL query caching.

Example:

Select * from table ;

Untitled-1

Above is the result of running a simple query on 2M rows, with query cache enabled. Here we see each time query is executed, Query cache is checked for existing result. If result not found, new cache is written to the query cache.  Which seems good but imaging the different scenario.

1. Each time there is modification in the table like update, edit on column e.t.c  query that is cached is invalid, so whole process of reading the query and writing to query has to perform again.

2. When query cache is written, its locks the cache of that query, so if one query is changing anything to the cache, no other query can not access it unless the lock is released, which results more response time. If there is higher frequency of updates on the query there will be more response time.

3. If you have millions of combination of query that can be generated in your database, caching query is not an option, as caching is only for few sec and holding millions of cached query to the memory, is not a possible options.

So, this summarized that query cache is not always good so when we use it ?

1. if we have same query is written in same piece of code very frequently,  then there is a less chance table is updated with in that milli- second, in that case we can use query caching.  Many framework related development of highly oops based development we are using same query many time in the code for example:

$result = $dataModel->find->id(‘5’);

// php code

if($dataModel->RelationalModel->status == 2)  {

// php code

}

In above example  dataModel is called twice, so same query will be executed in same code twice ( if object is not cached) , in that case query caching can be effective. The code is just a basic idea, its not a real code or working code of any kind.

So what we do?

1. If we have more read and less writes to the tables then query cache  can be efficient. Sites like forums, blogs, directory, news sites e.t.c has very less update frequency so query cache will be effective.

2. Limiting query cache size can be good option like few mega byte or few hundred mega byte relative to huge data size will limit the frequency of query caching.

3. if possible, disable query cache and test over all effect on performance can be good idea.

MySQL optimization depends on the database usage frequency and database data size, so testing different scenario with benchmark will be the best option. There is no hard written rule on optimization. But every time we optimize  MySQL server just enabling the query cache is not always the best option.