Object Oriented Programming in PHP Interview Questions & Answers

Object Oriented Programming in PHP Interview Questions

Are you preparing for an Object Oriented Programming in PHP job? Then you are at the right place for getting good interview questions that can land your dream job. Object Oriented Programming in PHP is a programming process to divide alike tasks into Class and Object to generate reusable code. As we can visualize things as different parts like a car made of different things like the wheel, steering, body, gear etc. There are several Object Oriented Programming in PHP jobs available in reputed organizations for various positions like Software Development Engineer, System Engineer, Software Developer, Lecturer, Application Developer, Consultant, Team Leader, Dot Net Developer etc. know more about Object Oriented Programming in PHP job interview Questions please visit our Object Oriented Programming in PHP job interview questions and answers page designed by wisdomjobs professional experts.

Object Oriented Programming In PHP Interview Questions And Answers

Object Oriented Programming in PHP Interview Questions
    1. Question 1. What Is Object Oriented Programming?

      Answer :

      Object-oriented programming (OOP) is a programming language model organized around objects rather than actions;

      Objects are instances of classes, are used to interact with one another.

      Following are few examples of object-oriented programming languages

      PHP, C++, Objective-C, Smalltalk, C#, Perl, Python, Ruby.

      The goals of object-oriented programming are:

      • Increased understanding.
      • Ease of maintenance.
      • Ease of evolution.

    2. Question 2. What Is Data Modeling?

      Answer :

      In class, we create multiple get/set function to get and set the data through the protected functions known as Data Modeling.

      class dataModel {    

          public function __set( $key, $value ) {

              $this->$key = $value;

          } 

      }

      Following are the benefits of Data Modeling:

      • It is very fast.
      • Smart way to  manipulation on data
      • No extra layer of logic 
      • Really flexible to be modeled per need 
      • Setters can explicitly define what data can be loaded into the object

    3. Question 3. What Is Difference Between Class And Interface?

      Answer :

      1. Interfaces do not contain business logic 
      2. You must extend interface to use.
      3. You can't create object of interface.

    4. Question 4. How Session - Cookie Works In Php?

      Answer :

      When a website open in new client machine(Browser), new sessionId is created and stored in php server and in client machine (In cookie).

      All data is store in PHP Server and cookie only have sessionId. When client send sessionId with request to the server, then server fetch the data corresponsing to that sessionId and retun to the browser.

    5. Question 5. What Are Some Of The Big Changes Php Has Gone Through In The Past Few Years?

      Answer :

      5.1 added PDO 

      5.3 - added namespace support 

    6. Question 6. What Is Polymorphism?

      Answer :

      It is simply "One thing, can use in different forms"

      For example, One car (class) can extend two classes (hond & Alta)

    7. Question 7. How To Load Classes In Php?

      Answer :

      • We can load a class with the use of "autoload" class.
      • If we want to change from default function autoload to testautload function.
      • we can do this with "spl_autoload_register" 
      • spl_autoload_register('kumar');

    8. Question 8. How To Call Parent Constructor?

      Answer :

      parent::__construct()

    9. Question 9. Are Parent Constructor Called Implicitly When Create An Object Of Class?

      Answer :

      No, Parent constructors are not called implicitly It must call this explicitly. But If Child constructors is missing then parent constructor called implicitly. 

    10. Question 10. What Happen, If Constructor Is Defined As Private Or Protected?

      Answer :

      If constructor declared as private, PHP through the following fatal error when create object.

      Fatal error: Call to private BaseClass::__construct() from invalid context in. If constructor declared as private, PHP through the following fatal error when create object. Fatal error: Call to protected BaseClass::__construct() from invalid context in.

    11. Question 11. What Happen, If New-style Constructor & Old-style Constructor Are Defined. Which One Will Be Called?

      Answer :

      New-Style constructor will called. But if New-Style constructor is missing, old style constructor will called. 

    12. Question 12. What Are Different Visibility Of Method/property?

      Answer :

      There are 3 types of visibility of method & property and are following

      Public: Can be accessed from same class method, child class and from outside of class.

      Protected : Can be accessed from same class method, child class.

      Private: Can be accessed from same class method only. 

      class TestClass

      {

          public $public = 'Public';

          protected $protected = 'Protected';

          private $private = 'Private';

       function printValue()

          {

              echo $this->public;

              echo $this->protected;

              echo $this->private;

          }

      }

      $obj = new TestClass();

      echo $obj->public; // Works

      echo $obj->protected; // Fatal error: Cannot access protected property TestClass::$protected in

      echo $obj->private; // Fatal error: Cannot access private property TestClass::$private in C:wampwwwarunclassclass.php on line 20

      $obj->printValue(); // Shows Public, Protected and Private 

    13. Question 13. What Is Scope Resolution Operator?

      Answer :

      The Scope Resolution Operator (also called Paamayim Nekudotayim) is double colon that allows access to static, constant, and overridden properties or methods of a class.

      Following are different uses Access to static

      • Acess the constant
      • Access the overridden properties of parent class
      • Access the overridden methods of a parent class

    14. Question 14. What Is Static Keyword In Php?

      Answer :

      • If we declare a Method or Class Property as static, then we can access that without use of instantiation of the class.
      • Static Method are faster than Normal method.
      • $this is not available within Static Method.
      • Static properties cannot be accessed through the object(i.e arrow operator)
      • Calling non-static methods with Scope Resolution operator, generates an E_STRICT level warning.
      • Static properties may only be initialized using a literal or constant value.
      • Static properties/Normal properties Can't be initialized using expressions value.

      class StaticClass

      {

          public static $staticValue = 'foo';

       

          public function staticValue() {

              return self::$my_static;

          }

      }

      echo StaticClass::$staticValue;

    15. Question 15. What Is Abstraction In Php?

      Answer :

      • Abstraction are defined using the keyword abstract .
      • PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated (create object).
      • To extend the Abstract class, extends operator is used.
      • You can inherit only one abstract class at one time extending.
      • Any class that contains one abstract method must also be declare as abstract. Methods defined as abstract simply declare the method's signature, they can't define the implementation.
      • All methods marked as abstract in the parent's class, declaration must be defined by the child.
      • additionally, these methods must be defined with the same (or a less restricted) visibility (Public,Protected & private).
      • Type hint & number of parameter must be match between parent & child class.

    16. Question 16. What Is Interface In Php?

      Answer :

      • Interfaces are defined using the interface keyword.
      • All methods declared in an interface must be public. Classes defined as Interface may not be instantiated(create object).
      • To extend the interface class, implements operator is used.
      • You can inherit number of interface class at the time of extending and number of abstract class separated by comma.
      • All methods in the interface must be implemented within a child class; failure to do so will result in a fatal error.
      • Interfaces can be extended like classes using the extends operator.
      • The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
      • Type hint & number of parameter must be match.

    17. Question 17. What Is Traits In Php?

      Answer :

      • Traits are a mechanism for code reuse in single inheritance.
      • A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. 
      • It is not possible to instantiate a Trait but 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.
      • Multiple Traits can be inserted into a class by listing them in the use statement, separated by commas(,).
      • If two Traits insert a method with the same name, a fatal error is produced.

      Example of Traits:

      class BaseClass{

          function getReturnType() {

              return 'BaseClass';

          }

      }

      trait traitSample {

          function getReturnType() {

              echo "TraitSample:";

              parent::getReturnType();

          }    

      }

      class Class1 extends BaseClass {

          use traitSample;   

      }

      $obj1 = new Class1();

      $obj1->getReturnType();//TraitSample:BaseClass

    18. Question 18. What Is Overloading?

      Answer :

      It is dynamically create method / properties and performed by magic methods. Overloading method / properties are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope, Means we you are calling a function which is not exist. None of the arguments of these magic methods can be passed by reference.

      In PHP, Overloading is possible http://200-530.blogspot.in/2013/04/oop-magic-methods.html

    19. Question 19. What Is Object Iteration?

      Answer :

      PHP provides a way for objects to be iterate through a list of items, for this we can use foreach. Only visible properties will be listed. 

      class TestClass{

          public $public='PublicVal';

          protected $protected='ProtectedVal';

          private $private='PrivateVal';

            function myfunc() {

              return 'func';

          }

           function iterateVisible(){

              foreach($this as $key => $value) {

                 print "$key => $valuen";

             }

          }

      }

      $obj=new TestClass();

      $obj->iterateVisible(); 

    20. Question 20. What Is Final Keyword In Php?

      Answer :

      PHP introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final.

      If the class itself is being defined final then it cannot be extended. If the function itself is being defined final then it cannot be extended. 

    21. Question 21. What Is Serialize Function In Php?

      Answer :

      It return string containing a byte-stream representation of any value that can be stored in PHP.

    22. Question 22. Comparing Objects?

      Answer :

      When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.

      When using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class

    23. Question 23. What Is Uml?

      Answer :

      UML stands for Unified Modeling Language.

      You can do following things with UML:

      • Manage project complexity.
      • create database schema.
      • Produce reports.

    24. Question 24. What Are Properties Of Object Oriented Systems?

      Answer :

      • Inheritance
      • Encapsulation of data
      • Extensibility of existing data types and classes
      • Support for complex data types
      • Aggregation
      • Association

Popular Interview Questions

All Interview Questions

All Practice Tests

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP7 Tutorial