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 relationshipbetween 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 whichthe class must adhere. This is achieved in C++ by using multiple  inheritance and deriving from two classes. PHP chose interfaces  as...

parent AND self PHP oops

 parent  AND self PHP oops self::refers to the current class and it is usually used to accessstatic 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::asopposed to the parent’s class name because it makes it easier  to change your class hierarchy...

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....

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) ...

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 wrec...

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. ...