Class execution action, classworkflow action

Source: Internet
Author: User

Class execution action, classworkflow action

1 <? Php 2/* 3 PHP 5 introduces abstract classes and methods. classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. methods defined as abstract simply declare the method's signature-they cannot define the implementation. 4 5 PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. If at least one method in a class is declared as abstract, the class must be declared as abstract. An abstract method only declares its call method (parameters) and cannot define its specific function implementation. 6 7 When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. for example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. furthermore the si Gnatures of the methods must match, I. e. the type hints and the number of required arguments must be the same. for example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature. this also applies to constructors as of PHP 5.4. before 5.4 constructor signatures cocould differ. 8. When inheriting an abstract class, the subclass must define all abstract methods in the parent class. In addition, the access control of these methods is required. Must be the same as in the parent class (or more loose ). For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, rather than private. In addition, the call method of the method must match, that is, the type and the number of required parameters must be consistent. For example, if a subclass defines an optional parameter and the declaration of the abstract method of the parent class does not exist, the declaration of the two does not conflict. This is also applicable to constructors from PHP 5.4. The constructor declaration before PHP 5.4 can be different. 9 10 */11 12 abstract class AbstractClass 13 {14 // Force Extending class to define this method 15 // Force the subclass to define these methods 16 abstract protected function getValue (); 17 abstract protected function prefixValue ($ prefix); 18 19 // Common method normal method (non-abstract method) 20 public function printOut () {21 print $ this-> getValue (). '<br>'; 22} 23} 24 25 class ConcreteClass1 extends AbstractClass 26 {27 protected function g EtValue () {28 return 'concreteclass1'; 29} 30 31 public function prefixValue ($ prefix) {32 return "{$ prefix }". 'concreteclass1'; 33} 34} 35 36 class ConcreteClass2 extends AbstractClass 37 {38 public function getValue () {39 return 'concreteclass2'; 40} 41 42 public function prefixValue ($ prefix) {43 return "{$ prefix }". 'concreteclass2'; 44} 45} 46 47/* 48 class ConcreteClass3 extends ACTC Lass 49 {50 private function getValue () {51 return 'concreteclass3'; 52} // Fatal error: Access level to ConcreteClass3: getValue () must be protected (as in class AbstractClass) or weaker in 53 54 public function prefixValue ($ prefix) {55 return "{$ prefix }". 'concreteclass3'; 56} 57} 58 */59 60 61 $ class1 = new ConcreteClass1; 62 $ class1-> printOut (); 63 echo $ class1-> prefixValue ('foo _'). '<br>'; 6 4 65 $ class2 = new ConcreteClass2; 66 $ class2-> printOut (); 67 echo $ class2-> prefixValue ('foo _'). '<br> '; 68 69 70 71 abstract class AbstractClassB 72 {73 // Our abstract method only needs to define the required arguments 74 // Our abstract method only needs to define the required parameter 75 abstract protected function prefixNameB ($ name ); 76} 77 78 class ConcreteClassB extends actclassb 79 {80 // Our child class may define optional ar Guments not in the parent's signature 81 // Our subclass can define the optional parameter 82 public function prefixNameB ($ name, $ separator = '. ') {83 if ($ name = 'pacman') {84 $ prefix = 'Mr '; 85} elseif ($ name = 'pacwoman ') {86 $ prefix = 'Mrs '; 87} else {88 $ prefix = ''; 89} 90 return" {$ prefix} {$ separator} {$ name }"; 91} 92} 93 94 $ classB = new ConcreteClassB; 95 echo $ classB-> prefixNameB ('pacman '),' <br> '; 96 Echo $ classB-> prefixNameB ('pacwoman '),' <br> '; 97 98 99/* 100 Object Interfaces 101 Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. 102 Interfaces are defined in the same was as a class, but with the interface keyword replacing the class keyword and without any of the methods having their cont Ents defined. 103 All methods declared in an interface must be public; this is the nature of an interface. 104 object interface 105 you can use interfaces to specify the methods that a class must implement, but you do not need to define the specific content of these methods. 106 interfaces are defined by the interface keyword, just like defining a standard class, but all the methods are empty. 107 all methods defined in the interface must be public, which is a feature of the interface. 108 109 110 111 implements 112 To implement an interface, the implements operator is used. all methods in the interface must be implemented within a class; failure to do so will result in a fatal error. classes may implement more than one interface if desired by separating each interface with a comma. 114 Note: 115 Prior to PHP 5.3.9, a class cocould not implement two interfaces that specified Method with the same name, since it wowould cause ambiguity. more recent versions of PHP allow this as long as the duplicate methods have the same signature. 116 Note: 117 Interfaces can be extended like classes using the extends operator. 118 Note: 119 The class implementing the interface must use the exact same method signatures as are defined in the interface. not doing so will result in a fatal Error. 120 Constants 121 It's possible for interfaces to have constants. interface constants works exactly like class constants blocks t they cannot be overridden by a class/interface that inherits them. 122 implement (implements) 123 to implement an interface, use the implements operator. Class must implement all methods defined in the interface, otherwise a fatal error is reported. Class can implement multiple interfaces, separated by commas. 124 Note: 125 when multiple interfaces are implemented, the methods in the interfaces cannot have duplicate names. 126 Note: The 127 interface can also be inherited by using the extends operator. 128 Note: The 129 class must use the same method as the method defined in the interface to implement the interface. Otherwise, a fatal error occurs. 130 constants can also be defined in the 131 interface. The use of interface constants and class constants is identical, but they cannot be overwritten by the quilt class or sub-interface. 132 133 134 */135 136 // Declare the interface 'itemplate '137 interface iTemplate138 {139 public function setVariable ($ name, $ var); 140 public function getHtml ($ template ); 141} 142 143 // Implement the interface144 // This will work145 146 class Template implements iTemplate147 {148 private $ vars = array (); 149 150 public function setVariable ($ name, $ var) 151 {152 $ this-> vars [$ name] = $ var; 153} 154 15 5 public function getHtml ($ template) 156 {157 foreach ($ this-> vars as $ name = >$ value) {158 $ template = str_replace ('{'. $ name. '}', $ value, $ template); 159} 160 return $ template; 161} 162} 163 164/* 165 class BadTemplate implements iTemplate166 {167 private $ var = array (); 168 public function setVariable ($ name, $ var) 169 {170 $ this-> vars [$ name] = $ var; 171} 172 Fatal error: Class BadTemplate contain S 1 abstract method and must therefore be declared abstract or implement the remaining methods (iTemplate: getHtml) 174 175 */176 177/* 178 class BadTemplate implements iTemplate179 {180 private $ vars = array (); 181 182 public function setVariable ($ name, $ var, $ echo) 183 {184 // Fatal error: Declaration of BadTemplate: setVariable () must be compatible with iTemplate: setVariable ($ name, $ var) 185 186 $ t His-> vars [$ name] = $ var; 187 echo $ echo; 188} 189 190 public function getHtml ($ template) 191 {192 foreach ($ this-> vars as $ name => $ value) {193 $ template = str_replace ('{'. $ name. '}', $ value, $ template); 194} 195 return $ template; 196} 197} 198 */199 200/* 201 class BadTemplate implements iTemplate202 {203 private $ vars = array (); 204 205 // Fatal error: Access level to BadTemplate :: setVariable () must be Public (as in class iTemplate) 206 207 protected function setVariable ($ name, $ var) 208 {209 210 $ this-> vars [$ name] = $ var; 211} 212 213 public function getHtml ($ template) 214 {215 foreach ($ this-> vars as $ name => $ value) {216 $ template = str_replace ('{'. $ name. '}', $ value, $ template); 217} 218 return $ template; 219} 220 221 */222 223 224 225 interface a227 {226 public function foo (); 229} 230 231 in Terface B extends a 232 {233 public function baz (Baz $ baz); 234} 235 236 class c implements b237 {238 public function foo () 239 {240 241} 242 243 public function baz (Baz $ baz) 244 {245 246} 247 248/* 249 Fatal error: Declaration of d: baz () must be compatible with B: baz (Baz $ baz) 251 252 class d implements b253 {254 public function foo () 255 {256 257} 258 259 public function baz (Foo $ foo) 260 {261 262} 263} 264 */265 266 267 // Multiple interface inheritance inherits Multiple interfaces 268 269 interface a1 270 {271 public function foo (); 272} 273 274 interface b1275 {276 public function bar (); 277} 278 interface c1 extends a1, b1280 {279 public function baz (); 282} 283 284 class d1 implements c1285 {286 public function foo () 287 {288} 289 290 public function bar () 291 {292} 293 294 public function baz () 295 {1, 296} 297} 298 299 // Interfaces with constants use the interface constant 300 Interface a2301 {302 const b2 = 'interface constant'; 303} 304 305 echo a2: b2; 306 307/* 308 Fatal error: Cannot inherit previusly-inherited or override constant b2 from interface a2 309 error syntax, because constants Cannot be overwritten. The concept of an interface constant is the same as that of a class constant. 310 311 class c2 implements a2312 {313 const b2 = 'class constant'; 314} 315 316 */

Http://php.net/

 

Summary:

0-The subclass must define all methods of the abstract class. The number of method parameters can be added, and access control is the same or weak. The implementation of the object interface also needs to implement all methods, but the number of parameters cannot be changed, and the access control must be public.

Question:

0-instances in the framework?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.