TAILIEUCHUNG - OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P14
OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P14:A number of years ago, before I started using PHP, I created dynamic web pages using C. This really wasn’t too different from some of the other options available at the time, though it seems almost unthinkable now. Creating a dynamic page meant outputting HTML from your script and recompiling that script if any changes needed to be made. | 13 MORE MAGIC METHODS So far we have come across the magic methods construct destruct and toString and have discussed them in detail. The remaining magic methods are_autoload __call clone get set sleep wakeup unset and As you might expect they only make sense in the context of object-oriented programming OOP . The syntactic element common to all magic methods is that they begin with a double underscore. They are all also usually invoked indirectly rather than directly. As we have seen the_construct method of a class is invoked when we use the new operator and a class name. If we have a class called MyClass that defines a constructor the statement m new MyClass indirectly calls the__construct method of this class. However the fact that all magic methods are called indirectly masks important differences between them. Having a uniform constructor for every class yields benefits when a parent constructor needs to be called but there is 1 There is also a magic method_set_state invoked by a call to the var_dump function. At this point there is minimal documentation regarding this method. For more information see http var_export. no intrinsic need for this method to be magic. For example in Java constructors bear the name of the class with no serious negative consequences. On the other hand destructors are a necessity and would seem to have to be magic. They are not invoked by any action of the developer but automatically when an object goes out of scope. Then there s the_toString method which is called implicitly whenever an object is displayed using print or echo a convenience method more than anything else. In any case the point is that the reasons for providing magic methods are various and in each case worth examining. In this chapter we will look at those magic methods that we haven t yet discussed. Related and complementary methods will be discussed together. get andset To set the context for this discussion remember that we .
đang nạp các trang xem trước