Object-oriented Programming with PHP4

WHILE PHP 4 IS not the poster child of a successful OOP implementation, it can nonetheless be used to build a viable object-oriented infrastructure—you just need to know where the pitfalls of an imperfect object model lie and work around them carefully. Even though PHP 5 has brought many changes to the way PHP handles objects and you may be tempted to simply ignore PHP 4’s capabilities, the truth is that OOP was embraced by many programmers who started developing their applications with the “old” PHP. This has resulted in a lot of OOP code out there—and the likelihood that you’ll find yourself working with it even before you make the jump to PHP 5 is very high. The OOP portion of the exam tests your knowledge not only of object-oriented programming in general, but also of the unique way PHP 4 implements it.

20 cards   |   Total Attempts: 188
  

Cards In This Set

Front Back
What is the construct used to define the blueprint of an object called? Your Answer: ____________________
A class is a blueprint of an object, which is an instance of a class.
At the end of the execution of the following script, which values will be stored in the $a->my_value array? (Choose 3)
The three correct answers are B, C and D. The set_value method of my_class will not work correctly because it uses the expression $this->$my_value, which is a “variable variable” that, under the circumstances will never correspond to any real property of the class.
How can you write a class so that some of its properties cannot be accessed from outside its methods? A. By declaring the class as private B. By declaring the methods as private C. It cannot be done D. By writing a property overloading method
Answer C is correct. In PHP 4, it is not possible to limit access to class members or properties. This can, however, be done in PHP 5, for example by declaring a property as private.
Which object-oriented pattern would you use to implement a class that must be instantiated only once for the entire lifespan of a script? A. Model-view-controller B. Abstract factory C. Singleton D. Proxy E. State
The Singleton Pattern is handy whenever only one instance of a particular class can exist at any given time (and, yes, in case you’re wondering, you should expect the exam to test you on the basics of patterns, too).
A class can be built as an extension of other classes using a process known as inheritance. In PHP, how many parents can a child class inherit from? A. One B. Two C. Depends on system resources D. Three E. As many as needed
Although other languages allow for multiple-inheritance, PHP’s object model is one of single-inheritance. Therefore, the correct answer is A.
What OOP construct unavailable in PHP 4 does the following script approximate?
This tidbit of code approximates the behaviour usually provided by an abstract method. If this class is inherited by another class and the my_funct method is called without being overridden in the child class, the code will throw an error. Naturally, this is only an approximation of the way abstract methods work, but it’s the best that can be done using PHP 4’s limited object model.
Assume that a class called testclass is defined. What must the name of its constructor method be? A. __construct B. initialize C. testclass D. __testclass E. Only PHP 5 supports constructors
Although PHP 5 has “unified” constructors (__construct()), in PHP 4 constructors are always methods whose name matches the class name. This means that, for a class called testclass, the constructor is Answer C, testclass().
How can a class override the default serialization mechanism for its objects? A. By implementing the __shutdown and __startup methods B. By calling register_shutdown_function() C. By implementing __sleep() and __wakeup() D. The default serialization mechanism cannot be overridden E. By adding the class to the output buffering mechanism using ob_start()
__sleep() and __wakeup() can be used to customize the serialization process of an object. The correct answer, therefore, is C.
In PHP 4, which object-oriented constructs from the following list are not available? - Abstract classes - Final classes - Public, private, protected (PPP) methods - Interfaces A. Abstract classes B. PPP methods C. Neither PPP methods nor interfaces D. None of the above are available E. All of the above are available
In PHP 4, there is no concept of any of the classic object-oriented constructs listed in the question (although many were introduced in PHP 5), so Answer D is correct.
How would you call the mymethod method of a class within the class itself? A. $self=>mymethod(); B. $this->mymethod(); C. $current->mymethod(); D. $this::mymethod(); E. None of the above are correct
In PHP, methods and properties of a class’ current instance are accessed from within its methods using the $this special variable. Answer B, therefore, is correct.
What will the following script output?
The right answer here is D—the script won’t output anything because my_class::_my_class() is not a valid constructor (did you notice the underscore at the beginning of the method name?). You might think that this is nothing but a trick question designed to see how much attention you’re paying during the exam… and you’re right. If you think about it, though, you’ll probably agree with us that many bugs occur because of misplaced characters. Therefore, this question is really designed to gauge your ability to catch mistakes in OOP code, rather than just to trick you.
What will the following script output?
Since in PHP 4 objects are treated the same way as scalar values, when $a is assigned to $b, the interpreter will create a copy of the object and, therefore, any value subsequently assigned to it will not affect the original object. Therefore, the correct answer is B. Note, however, that in PHP 5 the behaviour of this script would have been different (outputting 10)—but remember that the exam is about PHP 4, not PHP 5.
Consider the following script. What will it output?
This is a really tricky one. Upon first examination, it would seem that the constructor of my_class stores a reference to itself inside the $global_obj variable. Therefore, one would expect that, when we later change the value of $global_obj->my_value to 10, the corresponding value in $a would change as well. Unfortunately, the new operator does not return a reference, but a copy of the newly created object. Therefore, the script will output 5 and the correct answer is A.
Consider the following segment of PHP code. When it is executed, the string returned by the $eight_tenths->to_string method is 8 / 10 instead of the expected 4 / 5. Why? <?php class fraction { var $numerator; var $denominator; function fraction($n, $d) { $this->set_numerator($n); $this->set_denominator($d); } function set_numerator($num){ $this->numerator= (int)$num; } function set_denominator($num) { $this->denominator = (int)$num; } function to_string() { return "{$this->numerator} / {$this->denominator}"; } } function gcd($a, $b) { return ($b > 0) ? gcd($b, $a % $b) : $a; } function reduce_fraction($fraction) { $gcd= gcd($fraction->numerator, $fraction->denominator); $fraction->numerator /= $gcd; $fraction->denominator /= $gcd; } $eight_tenths = new fraction(8,10); /* Reduce the fraction */ reduce_fraction($eight_tenths); var_dump($eight_tenths->to_string()); ?> A. The reduce_fraction function must return a value B. The reduce_fraction function should accept integer values C. The gcd function is flawed D. You must pass the $eight_tenths object by-reference E. You cannot pass instances of objects to anything but methods
In PHP, objects that are passed to a function or method are, by default, passed by value, meaning the object used within the function is actually a copy of the object that was passed as a parameter. This unfortunate side effect means that any modifications to the object performed within the context of the function or method call will not apply to the original object outside of the function’s scope. In the case of Question 14, this means that the $eight_tenths object was never altered by the reduce_fraction function, while the $fraction object (the parameter) was. An object that may be modified inside a function should be always passed to it by reference: function reduce_fraction(&$fraction) Thus, the correct answer is D.
What does the following PHP code segment do? <?php require_once("myclass.php"); myclass::mymethod(); ?> A. Calls the mymethod method in the class statically. B. Creates and instance of myclass and calls the mymethod method. C. Generates a syntax error D. Defaults to the last-created instance of myclass and calls mymethod() E. Calls the function named myclass::mymethod()
The syntax shown in the question is used to call methods within a class from a static context. When methods are called from a static context, they behave like functions, and have no association with any existing instance of the class. The correct answer is A.