Recently asked OOPS PHP interview question and answer for fresher.

In this section we are providing you some Recently asked OOPS PHP interview question and answer for fresher. which will help you to win interview session easily. Candidates must read this section Also, review sample answers and advice on how to answer these typical interview questions. PHP is an important part of the web world, and every web developer should have the basic knowledge in PHP. Common PHP interview questions, which should help you become a best PHP coder. We hope you find these questions useful. If you are an interviewer, Take the time to read the common interview questions you will most likely be asked. Below are the Recently asked OOPS PHP interview question and answer for fresher.

What is Object Oriented Programming?

This is usually a pretty open ended question. You should understand classes (objects are instantiated classes), abstract classes, interfaces, methods, properties,inheritance, multiple inheritance as well as why OOP is helpful as compared to procedural programming.

what is the difference between a abstract class and an interface in PHP ?

In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.

Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.

Method of PHP interface must be public only. Method in abstract class in php could be public or protected both.

In abstract class you can define as well as declare methods. But in interface you can only defined your methods.

What is Polymorphism?

Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way.

Lets look at an example:

class BaseClass { public function myMethod() { echo “BaseClass method called”; } } class DerivedClass extends BaseClass { public function myMethod() { echo “DerivedClass method called”; } } function processClass(BaseClass $c) { $c->myMethod(); } $c = new DerivedClass(); processClass($c);

What is the Scope Resolution Operator?

The most common use for scope resolution is with the pseudo-class “parent”. For example, if you want a child object to call its parent’s __construct() function, you would use parent::__construct(). You can use the scope resolution operator to access the parent methods and properties. You can also use it to access constants and static properties.

public function __construct( $name )
{
   parent::__construct( $name );
   $this->setPetName( $name );
}

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

Final keywords indicates that the class or method cannot be extended.

Does PHP support multiple inheritance?

No. You should understand what multiple inheritance is.

What is data modeling?

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;
    } 
}

 

Are Parent constructors called implicitly when create an object of class?

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

What happen, If constructor is defined as private OR protected.?

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

What are different visibility of method/property?

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:\wamp\www\arun\class\class.php on line 20
$obj->printValue(); // Shows Public, Protected and Private 

Hope this article Recently asked OOPS PHP interview question and answer for fresher will help you to crack your interview.