Abstract class: Abstract classes cannot be instantiated. abstract classes are the same as other classes. variables and methods can be defined. abstract classes can also define an abstract method. abstract class methods are not executed, however, it may be executed in its derived class.
Abstract class: Abstract classes cannot be instantiated. abstract classes are the same as other classes. variables and methods can be defined. abstract classes can also define an abstract method. abstract class methods are not executed, however, it may be executed in its derived class.
Example 1: abstract class
-
- Abstract class foo {
- Protected $ x;
- Abstract function display ();
- Function setX ($ x ){
- $ This-> x = $ x;
- }
- }
- Class foo2 extends foo {
- Function display (){
- // Code
- }
- }
- ?>
_ Call:A special _ call () method is added to the PHP5 object. this method is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __call method will be automatically called.
Example 2 :__ call
-
- Class foo {
- Function _ call ($ name, $ arguments ){
- Print ("Did you call me? I'm $ name! ");
- }
- } $ X = new foo ();
- $ X-> doStuff ();
- $ X-> fancy_stuff ();
- ?>
This special method can be used to implement the "overload" action, so that you can check your parameters and pass parameters by calling a private method.
Example 3: Use _ call to implement "overload" action
-
- Class Magic {
- Function _ call ($ name, $ arguments ){
- If ($ name = 'Foo '){
- If (is_int ($ arguments [0]) $ this-> foo_for_int ($ arguments [0]);
- If (is_string ($ arguments [0]) $ this-> foo_for_string ($ arguments [0]);
- }
- } Private function foo_for_int ($ x ){
- Print ("oh an int! ");
- } Private function foo_for_string ($ x ){
- Print ("oh a string! ");
- }
- } $ X = new Magic ();
- $ X-> foo (3 );
- $ X-> foo ("3 ");
- ?>
_ Set and _ get
This is a great method. the __set and _ get methods can be used to capture variables and methods that do not exist in an object.
Example 4: _ set and _ get
-
- Class foo {
- Function _ set ($ name, $ val ){
- Print ("Hello, you tried to put $ val in $ name ");
- }
- Function _ get ($ name ){
- Print ("Hey you asked for $ name ");
- }
- }
- $ X = new foo ();
- $ X-> bar = 3;
- Print ($ x-> winky_winky );
- ?>