OOP PHP Interview Questions

Q. Explain what is object oriented programming language?
>>Object oriented programming language allows concepts such as modularity, encapsulation, polymorphism and inheritance.  Objects are said to be the most important part of object oriented language. Concept revolves around making simulation programs around an object. Organize a program around its data (object)& set well define interface to that data. i.e. objects and a set of well defined interfaces to that data. OOP is the common abbreviation for Object-Oriented Programming.  OOps have many properties such as DataHiding,Inheritence,Data Absraction,Data Encapsulation and many more.

Q. State some of the advantages of object oriented programming?
>>Some of the advantages of object oriented programming are as follows: –
a) A clear modular structure can be obtained which can be used as a prototype and it will not reveal the mechanism behind the design. It does have a clear interface.
b) Ease of maintenance and modification to the existing objects can be done with ease.
c) A good framework is provided which facilitates in creating rich GUI applications.

Q. Explain what is an object?
>>An object is a combination of messages and data. Objects can receive and send messages and use messages to interact with each other. The messages contain information that is to be passed to the recipient object.

Q.Explain about inheritance in OOPS?
>>Objects in one class can acquire properties of the objects in other classes by way of inheritance. Reusability which is a major factor is provided in object oriented programming which adds features to a class without modifying it. New class can be obtained from a class which is already present.
Q.Explain about the relationship between object oriented programming and databases?
>>Object oriented programming and relational database programming are almost similar in software engineering. RDBMS will not store objects directly and that’s where object oriented programming comes into play. Object relational mapping is one such solution.

Q. Explain about a class in OOP?
>>In Object oriented programming usage of class often occurs. A class defines the characteristics of an object and its behaviors. This defines the nature and functioning of a specified object to which it is assigned. Code for a class should be encapsulated.

Q. Explain the usage of encapsulation?
>>Encapsulation specifies the different classes which can use the members of an object. The main goal of encapsulation is to provide an interface to clients which decrease the dependency on those features and parts which are likely to change in future. This facilitates easy changes to the code and features.
Q. Explain about abstraction?
>>Abstraction can also be achieved through composition. It solves a complex problem by defining only those classes which are relevant to the problem and not involving the whole complex code into play.

Q. Explain what a method is?
>>A method will affect only a particular object to which it is specified. Methods are verbs meaning they define actions which a particular object will perform. It also defines various other characteristics of a particular object.

Q. What is the use of “Final class” and can a final class be an abstract?

>>The “Final” keyword is used to make the class uninheritable. So the class or it’s methods can not be overridden.
1 final class Class1 {
2 // …
3 }
5 class FatalClass extends Class1 {
6 // …
7 }
9 $out= new FatalClass();

An Abstract class will never be a final class as an abstract class must be extendable.
Advanced level, PHP Questions
abstract class, oop

Q. What is the Difference between Abstract class and interface.

>>Abstract classes are extended, whilst an interface is implemented. A class should be really only be extended if it first the “is a” principle. e.g. Dog is a Pet, therefore it makes sense that Dog should extend Pet. Interfaces should be used to provide a common interface to use in situations, in PHP you can implement ArrayAccess, which requires you to implement certain functions.

Use abstract classes when other classes should be created that are a specialization of another class. In the case mentioned an abstract class is no different to an interface, however an abstract class can create non-abstract functions that provide default behaviour.

Use interfaces allow for multiple inheritance, most useful for defining “mixins”, such as ArrayAccess, Serializable or Sortable, where the class isn’t a specialization but are more attributes of a class.

Q. Explain abstract class and its behaviour?
>>Abstract class is defined as a solitary entity, so other classes can inherit from it. An abstract class can not be instantiated, only the subclasses of it can have instances. Following is one of the best examples to explain the use of an abstract class and the behavior of it.
view source
01 class Fruit {
02 private $color;
04 public function eat() {
05 //chew
06 }
08 public function setColor($c) {
09 $this->color = $c;
10 }
11 }
13 class Apple extends Fruit {
14 public function eat() {
15 //chew until core
16 }
17 }
19 class Orange extends Fruit {
20 public function eat() {
21 //peel
22 //chew
23 }
24 }

Now taste an apple
view source
1 $apple = new Apple();
2 $apple->eat();

What’s the taste of it? Obviously it’s apple

Now eat a fruit
view source
1 $fruit = new Fruit();
2 $fruit->eat();

What’s the taste of it? It doesn’t make any sense. does it? Which means the class fruit should not be Instantiable . This is where the abstract class comes into play
1 abstract class Fruit {
2 private $color;
4 abstract public function eat()
6 public function setColor($c) {
7 $this->color = $c;
8 }
9 }

Q. how to know user has read the email?

>>Using Disposition-Notification-To: in mailheader we can get read receipt.

Add the possibility to define a read receipt when sending an email.

It’s quite straightforward, just edit email.php, and add this at vars definitions:

var $readReceipt = null;

And then, at ‘createHeader’ function add:

if (!empty($this->readReceipt)) { $this->__header .= ‘Disposition-Notification-To: ‘ . $this->__formatAddress($this->readReceipt) . $this->_newLine; }

Q. What is a Constructor?

>>A constructor is for initializing an object’s private state prior to it being used. Any variables that need setting, objects that need making, or anything that needs to be done before an object is usable should be done in the constructor if possible.

Q. What are magic methods?

>>Magic methods are the members functions that is available to all the instance of class Magic methods always starts with “__”. Eg. __construct All magic methods needs to be declared as public To use magic method they should be defined within the class or program scope Various Magic Methods used in PHP 5 are: __construct() __destruct() __set() __get() __call() __toString() __sleep() __wakeup() __isset() __unset() __autoload() __clone()

Q.  what is magic quotes?

>>Magic Quotes is a process that automagically escapes incoming data to the PHP script. It’s preferred to code with magic quotes off and to instead escape the data at runtime, as needed. This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

Q.  diff b/w php4 & php5 ?

>> 1. In PHP5 1 Implementation of exceptions and exception handling

2. Type hinting which allows you to force the type of a specific argument as object, array or NULL

3. Overloading of methods through the __call function

4. Full constructors and destructors etc through a __constuctor and __destructor function

5. __autoload function for dynamically including certain include files depending on the class you are trying to create.

6 Finality : can now use the final keyword to indicate that a method cannot be overridden by a child. You can also declare an entire class as final which prevents it from having any children at all.

7 Interfaces & Abstract Classes

8 Passed by Reference : In PHP4, everything was passed by value, including objects. This has changed in PHP5 — all objects are now passed by reference.

9 An __clone method if you really want to duplicate an object

Q. In php4 can you define a class? how to call class in php4? can you create object in php4?

>> yes you can define class and can call class by creating object of that class. but the diff b/w php4 & php5 is that in php4 everything was passed by value where as in php5 its by reference. And also any value change in reference object changes the actucal value of object also. And one more thing in introduction of __clone object in PHP5 for copying the object.

Posted By:

AentteQ Software Technologies

%d bloggers like this: