PHP OOP Interview Questions And Answers

What is PHP?

PHP: Hypertext Preprocessor is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

What are the advantages of object oriented programming?

  1. Code Resusability: it can be acheived through inheritance and traits
  2. Modularity: it can be acheived through breaking large code into small modules, Modularity reduces complexity.
  3. Flexibility: it can be acheived through polymorphism
  4. Maintainability: it is to maintian code which follow Object Oriented Programming Concepts.
  5. Security: it can be acheived through Encapsulation
  6. Testability: it is easy to test.

What is Encapsulation?

Wrapping up member variables and methods together into a single unit (i.e. Class) is called Encapsulation.

  1. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them.
  2. Visibility is the mechanism for encapsulation.

What is the key difference between concrete class and abstract class?

Concrete classes are those classes which has to declare body of abstract methods which extends or implements from abstract class or interface

OR

Abstract classes usually have partial or no implementation. On the other hand, Concrete classes always have full implementation of its behavior. Unlike Concrete classes, Abstract classes cannot be instantiated.

What is final keyword?

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with the final. If the class itself is being defined final then it cannot be extended.

What is Traits in PHP?

Traits are a mechanism for code reuse in single inheritance languages such as PHP. Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
  1. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.
  2. The semantics of the combination of Traits and classes is defined in a way which reduces complexity and avoids the typical problems associated with multiple inheritance and Mixins.
  3. A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way.
  4. It is not possible to instantiate a Trait but an addition to traditional inheritance. It is intended to reduce some limitations of single inheritance to reuse sets of methods freely in several independent classes living in different class hierarchies.
  5. Multiple Traits can be inserted into a class by listing them in the use statement, separated by commas(,).
  6. If two Traits insert a method with the same name, a fatal error is produced.

Note: Multiple traits can be used in a class by listing them in the use statement, separated by commas.

 

What is Type Hinting in PHP?

Type Hinting is used to specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration. It is beneficial because it results in better code organization and improved error messages.

Suppose we want to force a function to get only arguments of the type array, then we can simply put the array keyword before argument’s name.

 

What is Overloading  and Overriding?

  1. Overloading is defining functions/methods that have same signatures with different parameters in the same class.
  2. Overriding is redefining parent class functions/methods in child class with same signature. So, basically the purpose of overriding is to change the behavior of your parent class method.

 

What are constructor and destructor in PHP ?

PHP constructor and destructor are special type functions that are automatically called when a PHP class object is created and destroyed.

Generally, Constructor is used to initializing the private variables for class and Destructors to free the resources created /used by the class.

Here is a sample class with a constructor and destructor in PHP.

<?php
class Foo {
   
    private $name;
    private $link;

    public function __construct($name) {
        $this->name = $name;
    }

    public function setLink(Foo $link){
        $this->link = $link;
    }

    public function __destruct() {
        echo 'Destroying: '. $this->name;
    }
}
?>

What is namespaces in PHP?

PHP Namespaces provide a way of grouping related classes, interfaces, functions and constants.

# define namespace and class in namespace
namespace Modules\Admin\;
class CityController {
}
# include the class using namesapce
use Modules\Admin\CityController ;

In PHP all functions starting with __ names are magical functions/methods. Magical methods always lives in a PHP class.The definition of magical function are defined by programmer itself.

Here are list of magic functions available in PHP

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo() .

 

What is the meaning of a final class and a final method?

The final keyword in a method declaration indicates that the method cannot be overridden by subclasses. A class that is declared final cannot be subclassed. This is particularly useful when we are creating an immutable class like the String class.Properties cannot be declared final, only classes and methods may be declared as final.

What are the popular content management systems (CMS) in php?

  • WordPress: WordPress is a free and open-source content management system (CMS) based on PHP & MySQL. It includes a plug-in architecture and template system. It is mostly connected with blogging but supports another kind of web content, containing more traditional mailing lists and forums, media displays, and online stores.
  • Joomla: Joomla is a free and open-source content management system (CMS) for distributing web content, created by Open Source Matters, Inc. It is based on a model-view-controller web application framework that can be used independently of the CMS.
  • Magento: Magento is an open source E-trade programming, made by Varien Inc., which is valuable for online business. It has a flexible measured design and is versatile with many control alternatives that are useful for clients. Magento utilizes E-trade stage which offers organization extreme E-business arrangements and extensive support network.
  • Drupal: Drupal is a CMS platform developed in PHP and distributed under the GNU (General Public License).

WHAT ARE THE POPULAR FRAMEWORKS IN PHP?

  • Laravel
  • CakePHP
  • CodeIgniter
  • Yii 2
  • Symfony
  • Zend Framework etc.

 

How many types of array are there in php?

There are three types of array in PHP:
  1. Indexed array: an array with a numeric key.
  2. Associative array: an array where each key has its specific value.
  3. Multidimensional array: an array containing one or more arrays within itself.

What are the different errors in PHP?

In PHP, there are three types of runtime errors, they are:

Warnings: 
These are important errors. Example: When we try to include () file which is not available. These errors are showed to the user by default but they will not result in ending the script.

Notices:
These errors are non-critical and trivial errors that come across while executing the script in PHP. Example: trying to gain access the variable which is not defined. These errors are not showed to the users by default even if the default behavior is changed.

Fatal errors: 
These are critical errors. Example: instantiating an object of a class which does not exist or a non-existent function is called. These errors results in termination of the script immediately and default behavior of PHP is shown to them when they take place. Twelve different error types are used to represent these variations internally.

What is overriding in PHP?

Overloading is defining functions that have similar signatures, yet have different parameters. Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that method. In PHP, you can only overload methods using the magic method __call.

What is encapsulation in PHP?

Encapsulation is an OOP (Object Oriented Programming) concept in PHP. Wrapping some data in single unit is called Encapsulation. Second advantage of encapsulation is you can make the class read only or write only by providing setter or getter method. Capsule is best example of Encapsulation.

What is polymorphism in PHP?

Polymorphism is a long word for a very simple concept. Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.

What is the use of magic function in PHP?

PHP provides a number of ‘magic’ methods that allow you to do some pretty neat tricks in object oriented programming. These methods, identified by a two underscore prefix (__), function as interceptors that are automatically called when certain conditions are met.

 

What is the use of die in PHP?

The die() function prints a message and exits the current script. This function is an alias of the exit() function.

 

What is stdClass in PHP?

stdClass is a PHP generic empty class and stdClass is used to create the new Object.  stdClass is a kind of Object in Java or object in Python but not actually used as universal base class.

What is the difference between php traits vs interfaces?

The main difference is that, with interfaces, you must define the actual implementation of each method within each class that implements said interface, so you can have many classes implement the same interface but with different behavior, while traits are just chunks of code injected in a class.

Leave a Reply

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