Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

PHP interview questions.

Difference between abstract class and interface ?
Abstract Class :
1. For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation.
2. The Abstract methods can declare with Access modifiers like public, internal, protected.
3. Abstract class can contain variables and concrete methods.
4. A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class.
Interface :
1. For interface all the methods by default are abstract methods only.
2. All methods declared in an interface must be public.
3. Interfaces cannot contain variables and concrete methods except constants.
4. A class can implement many interfaces and Multiple interface inheritance is possible.

What is MVC ?
MVC stands for M – Model, V – View and C – Controller. Model deals with the database query part, View deals with the design part and the Controller deals with the business logic part. Using MVC there are lot of advanctages likes, we can modify the View withoutaffecting the internal implementation logic of the portal.

PHP Session, how it works ?
Sessions works as a global variable in PHP. There are 2 parts associated with sessions, one is the session id, which are stored in client side cookies. The second part is saved in server which has the entire data associated with the session id. Where sessions are saved is basically depended on the “session.save_path” value which is configured in PHP.  Usually session is saved in files, this can also be saved in db.

what is name spaces?
Name spaces are infact hierarchical named code blocks, which contains regular PHP codes. These were introduced into PHP from 5.3 onwards. The real use of namespace is that, we can have same named methods, objects and properties in a same code block. These would be actually under each namespaces.
eg:
namespace abc; function myFunc(){ echo “abc”; }
namespace xyz; function myFunc(){ echo “xyz”; }
\abc\myFunc();  => will echo abc
\xyz\myFunc();  => will echo xyz

difference between GET and POST?
GET method is normally used for getting information and POST method is used for posting information. GET is not secure  to post information, in the sense that it passes all information visibly through the request url. This can be cached and may have performance issues too. In POST, all data and send along the POST body and will be visible. There is limitation to the max size of information that can be passed through GET, normally browser depended. In POST there is no limitation to the amount of data that can be sent to the server.

auto load function with php?
__autoload(), is a magic function added with php. To explain with an example,

myClass.php
<?php
class myClass{
function __construct(){
echo “This is for testing…”;
}
}
?>

index.php
<?
function __autoload($className){
$filename = $className.”php”;
include_once($filename);
}

$newObj = new myClass();
?>

When we run the index.php, the output will be;
> This is for testing…
Note there is not include explicitly mentioned, before the object is created. The __autoload() does the work for us.

what is scope resolution operator ?
‘::’ is the scope resolution operator used in php. It is used in object-oriented programming when you want to be specific about what kind of function you are calling. Consider the following example.

class abc extends xyz{
public __construct(){

}
}
Now when we create object of abc, child constructor is normally called. We have the option to call the constructor of xyz too, it is parent::__construct(). Here we are specifying of which class we are calling the constructor.

php design patterns
Design patterns are solutions to commonly occurring problems in software design. There are different design patterns used in industry, some of them are;
singletone method, decorator pattern, factory pattern etc..

what is ob_start ?
ob_start() is used for output buffering. This saves all the data that are outputted in the buffer. To get the buffered content we need to use, ob_get_contents(). This function is really helpful when dealing with the error that “headers are already send”. This error is normally shown since there are some blank spaces in the start of end of the code, which had some kind of url redirect called within.

what does &$var means ?
&$var means passing by reference. If the value of this variable is changed in any location, it affects the original value too.

Property of final class and final method ?
Final keyword when applied to a class, it then cannot be extended. When final keyword is applied with a method, it then cannot be overridden.

what are magic methods ?
Magic methods are those that starts with a “__”, double underscore. eg; __construct(), __call(), __get() etc. Developer needs to write the implementation details for these methods, but calling need not be done. These functions are called behind the screen by php itself. When an object of a class is created, php automatically calls the __constructor() method of the calls (if not defined/decleared, no error will be thrown.)
The complete set of magic methods as in php5 are;
__get() , __set() , __autoload , __sleep() , __wakeup() , __construct() , __destruct() , __isset() , __unset() , __clone , __call ,__toString()

what is pass by value and what is pass by reference? declared&$var is pass by reference. In this, we are passing the memory reference to the variable so that any update in the value will be reflected back to the original value. $var is pass by value. In this if the value of the passed variable is changed in any of the sub-functions, the original value is not updated.

difference between $var and $$var?
I will explain this with an example. Suppose;
$a = 5;
$var = “a”;
$$var will be equal to 5.

“$$” means value of ( value of (variable))

What is PEAR?
PEAR – PHP Extension and Application Repository, is a framework and distributed system of reusable PHP components. PEAR pakages are distributed as gzipped tar files and they do not have code dependencies as such.

What is bubbling in javascript? 
In bubbling an event is first captured by the inner most layer and then propagated to the outer most layers.
*Bubbling and Capturing are the two methods of event propagation in HTML DOM.

What are the 2 layers present in the Model layer, in Symfony framework?
Object Relational Mapping and Data Abstraction layer.

What is ORM?
Object Relational Mapping (ORM) is used in converting the objects to normal strings and reverse.

Advertisements