Showing posts with label OOPS-Interview-Questions. Show all posts
Showing posts with label OOPS-Interview-Questions. Show all posts

PHP INTERFACES


Class inheritanceenables you to describe a parent-child relationship
between classes. For example, you might have a base class
 Shapefrom which both Squareand Circlederive. However,
 you might often want to add additional “interfaces” to classes,
 basically meaning additional
 contracts to which
the class must adhere. This is achieved in C++ by using multiple
 inheritance and deriving from two classes. PHP chose interfaces
 as an alternative to multiple inheritance, which allows you to specify
 additional contracts a class must follow. An interface is declared
similar to a class but only includes function prototypes without
implementation and constants. Any class that “implements”
 this interface automatically has the interface’s constants defined and,
as the implementing class, needs to supply the function definitions for the
interface’s function prototypes that are all abstractmethods unless you
declare the implementing class as abstract.

abstract class Shape {
function setCenter($x, $y) {
$this->x = $x;
$this->y = $y;
}
abstract function draw();

protected $x, $y;
}
class Square extends Shape {
function draw()
{
// Here goes the code which draws the Square
...
}
}
class Circle extends Shape {
function draw()
{
// Here goes the code which draws the Circle
...
}
}
You can see that the draw()abstract method does not
 contain any code.

To implement an interface, use the following syntax:
class A implements B, C, ... {
...
}

Classes that implement an interface have an instance of a relationship
 with the interface; for example, if class Aimplements interface
 myInterface, the following results in '$obj is-A myInterface'printing:

$obj = new A();
if ($obj instanceof myInterface) {
print '$obj is-A myInterface';
}

parent AND self PHP oops

 parent  AND self PHP oops

self::refers to the current class and it is usually used to access
static members, methods, and constants. parent::refers to the
 parent class and it is most often used when wanting to call the
 parent constructor or methods. It may also be used to access
members and constants. You should use parent::as
opposed to the parent’s class name because it makes it easier
 to change your class hierarchy because you are not hard-coding
 the parent’s class name.

The following example makes use of both parent::and self::for
 accessing the Childand Ancestorclasses:

class Ancestor {
const NAME = "Ancestor";
function __construct()
{
print "In " . self::NAME . " constructor\n";
}
}
class Child extends Ancestor {
const NAME = "Child";
function __construct()
{
parent::__construct();
print "In " . self::NAME . " constructor\n";
}
}
$obj = new Child();

The previous example outputs
In Ancestor constructor
In Child constructor
Make sure you use these two class names whenever possible.

Static Methods-PHP

 PHP supports declaring methods as static.
Whatthis means is that your static methods are part of the
 class and are not bound to any specific object instance and
 its properties. Therefore, $this isn’t accessible in these methods,
 but the class itself is by using selfto access it. Because
static methods aren’t bound to any specific object, you can
 call them without creating an object instance by using the
class_name::method()syntax. You may also call them from an
 object instance using $this->method(), but $thiswon’t
be defined in the called method. For clarity, you should use self::method()
instead of $this->method().

class PrettyPrinter {
static function printHelloWorld()
{
print "Hello, World";
self::printNewline();
}
static function printNewline()
{
print "\n";
}
}
PrettyPrinter::printHelloWorld();

The example prints the string "Hello, World"followed by a newline.
Although it is a useless example, you can see that printHelloWorld()can be
called on the class without creating an object instance using the class name,
and the static method itself can call another static method of the
class printNewline()using the self::notation. You may call a parent’s
 static method by using the parent::notation.

Static Properties

As you know by now, classes can declare properties. Each instance of the
class,object has its own copy of these properties. However, a class can
also contain static properties. Unlike regular properties, these belong to
the class itself and not to any instance of it.

class propertiesas opposed to object or instance properties. You can also
think of static properties as global variables that sit inside a class but are
accessible from anywhere via the class.
Static properties are defined by using the static keyword:
class MyClass {
static $myStaticVariable;
static $myInitializedStaticVariable = 0;
}
To access static properties, you have to qualify the property name with
the class it sits in

MyClass::$myInitializedStaticVariable++;
print MyClass::$myInitializedStaticVariable;

how access a method in the parent class that's been overridden in the child?

Prefix parent:: to the method name:
class shape {
    function draw( ) {
        // write to screen
    }
}

class circle extends shape {
   function draw($origin, $radius) {
      // validate data
      if ($radius > 0) {
          parent::draw( );
          return true;
      }

      return false;
   }
}

how execute different code depending on the number and type of arguments passed to a method?

PHP doesn't support method polymorphism as a built-in feature. However, you can emulate it using various type-checking functions. The following combine( ) function uses is_numeric(), is_string(), is_array(), and is_bool():
// combine() adds numbers, concatenates strings, merges arrays,
// and ANDs bitwise and boolean arguments
function combine($a, $b) {
    if (is_numeric($a) && is_numeric($b)) {
        return $a + $b;
    }

    if (is_string($a)  && is_string($b))  {
        return "$a$b";
    }

    if (is_array($a)   && is_array($b))   {
        return array_merge($a, $b);
    }

    if (is_bool($a)    && is_bool($b))    {
        return $a & $b;
    }

    return false;
}

want to link two objects, so when you update one, you also update the other?

Use =& to assign one object to another by reference:
$adam = new user;
$dave =& $adam;

how eliminate an object?

Objects are automatically destroyed when a script terminates. To force the destruction of an object, use unset( ):
$car = new car; // buy new car
...
unset($car);    // car wreck

how create a new instance of an object?

Define the class, then use new to create an instance of the class:
class user {
    function load_info($username) {
       // load profile from database
    }
}

$user = new user;
$user->load_info($_REQUEST['username']);

function overloading?

Function overloading is defined as a normal function, but it has the ability to perform different tasks. It allows creation of several methods with the same name which differ from each other by type of input and output of the function.
Example
void add(int& k, int& s);
void add(double& k, double& s);
void add(struct bob& k, struct bob& s);

friend function?

Friend declaration can be placed anywhere in the class declaration.
Friend function that is a "friend" of a given class is allowed access to public, private, or protected data in that class.

default access specifier in a class definition?

Private access specifier is used in a class definition.

Difference between class and an object?

An object is an instance of a class. Objects hold any information , but classes don’t have any information. Definition of properties and functions can be done at class and can be used by the object.