This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Object Advanced Features – 3”.
1. Which one of the following method is invoked when a value is assigned to an undefined property?
a) __get()
b) __set()
c) __isset()
d) __call()
View Answer
Explanation: The __set() method is invoked when client code attempts to assign to an undefined property. It is passed two arguments: the name of the property, and the value the client is attempting to set.
2. Which one of the following method is invoked when an undefined method is called by client code?
a) __get()
b) __isset()
c) __unset()
d) __call()
View Answer
Explanation: The __call() method is probably the most useful of all the interceptor methods. The __call() method can be useful for delegation. Delegation is the mechanism by which one object passes method invocations on to a second.
3. Which method introduced in PHP 5, is invoked just before an object is a garbage collected?
a) __collect()
b) __garbage()
c) __destruct()
d) __destructor()
View Answer
Explanation: You can use this method to perform any final cleaning up that might be necessary. Imagine, for example, a class that saves itself to a database when so ordered. I could use the __destruct() method to ensure that an instance saves its data when it is deleted.
4. Which one of the following PHP statements is true?
class CopyMe {}
$first = new CopyMe();
$second = $first;
a) In PHP 4: $second and $first are 2 distinct objects
b) In PHP 5: $second and $first are 2 distinct objects
c) In PHP 4: $second and $first refer to one object
d) None of the mentioned
View Answer
Explanation: None.
5. Which keyword must be added before $first variable on the third line of the above question to make $second and $first as distinct objects in PHP 5?
a) copy
b) clone
c) cut
d) Can’t add any word to make them distinct
View Answer
Explanation: Clone operates on an object instance, producing a by-value copy.
6. What will be the output of the following PHP code? (Before the version PHP 5.2)
class StringThing {}
$st = new StringThing();
print $st;
a) Object Not Found
b) Object id #1
c) PHP Catchable fatal error
d) Cannot initialize object
View Answer
Explanation: Since PHP 5.2, this code will produce an error like this: PHP Catchable fatal error: Object of class StringThing could not be converted to string.
7. What will be the output of the following PHP code?
class Person
{
function getName() { return "Bob"; }
function getAge() { return 44; }
function __toString() {
$desc = $this->getName();
$desc .= " (age ".$this->getAge().")";
return $desc;
}
}
$person = new Person();
print $person;
a) Object Not Found
b) PHP Catchable fatal error
c) BOB (age 44)
d) BOB
View Answer
Explanation: By implementing a __toString() method, you can control how your objects represent themselves when printed. The method is invoked automatically when your object is passed to print or echo, and its return value is substituted.
8. __clone() is run on the ___ object.
a) original
b) pseudo
c) external
d) copied
View Answer
Explanation: __clone() is run on the copied object and not the original.
9. Which method is invoked when an undefined property is accessed?
a) __get()
b) __isset()
c) __unset()
d) __undefined()
View Answer
Explanation: None.
10. What will be the output of the following PHP code?
class Checkout
{
final function totalize()
{
// calculate bill
}
}
class IllegalCheckout extends Checkout
{
final function totalize()
{
// change bill calculation
}
}
a) PHP Fatal error: Class IllegalCheckout may not inherit from final class
b) Value of the bill calculated
c) PHP Fatal error: Cannot find object
d) PHP Fatal error: Cannot override final method
View Answer
Explanation: A final class cannot be subclassed. Less drastically, a final method cannot be overridden.
Sanfoundry Global Education & Learning Series – PHP Programming.
To practice all questions on PHP Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Programming Internship
- Check MCA Books
- Practice MCA MCQs