Rumah > php教程 > php手册 > teks badan

Class Abstraction -- Object Interfaces

WBOY
Lepaskan: 2016-08-15 16:49:43
asal
1112 orang telah melayarinya
<span style="color: #008080;">  1</span> <span style="color: #000000;">php
</span><span style="color: #008080;">  2</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;">  3</span> <span style="color: #008000;">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. 
</span><span style="color: #008080;">  4</span> 
<span style="color: #008080;">  5</span> <span style="color: #008000;">PHP 5 支持抽象类和抽象方法。定义为抽象的类不能被实例化。任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的。被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现。 
</span><span style="color: #008080;">  6</span> 
<span style="color: #008080;">  7</span> <span style="color: #008000;">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 signatures 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 could differ.
</span><span style="color: #008080;">  8</span> <span style="color: #008000;">继承一个抽象类的时候,子类必须定义父类中的所有抽象方法;另外,这些方法的访问控制必须和父类中一样(或者更为宽松)。例如某个抽象方法被声明为受保护的,那么子类中实现的方法就应该声明为受保护的或者公有的,而不能定义为私有的。此外方法的调用方式必须匹配,即类型和所需参数数量必须一致。例如,子类定义了一个可选参数,而父类抽象方法的声明里没有,则两者的声明并无冲突。 这也适用于 PHP 5.4 起的构造函数。在 PHP 5.4 之前的构造函数声明可以不一样的。 
</span><span style="color: #008080;">  9</span> 
<span style="color: #008080;"> 10</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 11</span> 
<span style="color: #008080;"> 12</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> AbstractClass
</span><span style="color: #008080;"> 13</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 14</span>     <span style="color: #008000;">//</span><span style="color: #008000;">Force Extending class to define this method
</span><span style="color: #008080;"> 15</span> <span style="color: #008000;">    // 强制要求子类定义这些方法</span>
<span style="color: #008080;"> 16</span>     <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue();
</span><span style="color: #008080;"> 17</span>     <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> prefixValue(<span style="color: #800080;">$prefix</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 18</span> 
<span style="color: #008080;"> 19</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> Common method 普通方法(非抽象方法)</span>
<span style="color: #008080;"> 20</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> printOut(){
</span><span style="color: #008080;"> 21</span>         <span style="color: #0000ff;">print</span> <span style="color: #800080;">$this</span>->getValue().'<br>'<span style="color: #000000;">;
</span><span style="color: #008080;"> 22</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 23</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 24</span> 
<span style="color: #008080;"> 25</span> <span style="color: #0000ff;">class</span> ConcreteClass1 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> AbstractClass
</span><span style="color: #008080;"> 26</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 27</span>     <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue(){
</span><span style="color: #008080;"> 28</span>         <span style="color: #0000ff;">return</span> 'ConcreteClass1'<span style="color: #000000;">;
</span><span style="color: #008080;"> 29</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 30</span> 
<span style="color: #008080;"> 31</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> prefixValue(<span style="color: #800080;">$prefix</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 32</span>         <span style="color: #0000ff;">return</span> "{<span style="color: #800080;">$prefix</span>}".'ConcreteClass1'<span style="color: #000000;">;
</span><span style="color: #008080;"> 33</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 34</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 35</span> 
<span style="color: #008080;"> 36</span> <span style="color: #0000ff;">class</span> ConcreteClass2 <span style="color: #0000ff;">extends</span><span style="color: #000000;"> AbstractClass
</span><span style="color: #008080;"> 37</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 38</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getValue(){
</span><span style="color: #008080;"> 39</span>         <span style="color: #0000ff;">return</span> 'ConcreteClass2'<span style="color: #000000;">;
</span><span style="color: #008080;"> 40</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 41</span> 
<span style="color: #008080;"> 42</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> prefixValue(<span style="color: #800080;">$prefix</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 43</span>         <span style="color: #0000ff;">return</span> "{<span style="color: #800080;">$prefix</span>}".'ConcreteClass2'<span style="color: #000000;">;
</span><span style="color: #008080;"> 44</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 45</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 46</span> 
<span style="color: #008080;"> 47</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;"> 48</span> <span style="color: #008000;">class ConcreteClass3 extends AbstractClass
</span><span style="color: #008080;"> 49</span> <span style="color: #008000;">{
</span><span style="color: #008080;"> 50</span> <span style="color: #008000;">    private function getValue(){
</span><span style="color: #008080;"> 51</span> <span style="color: #008000;">        return 'ConcreteClass3';
</span><span style="color: #008080;"> 52</span> <span style="color: #008000;">    }//Fatal error: Access level to ConcreteClass3::getValue() must be protected (as in class AbstractClass) or weaker in 
</span><span style="color: #008080;"> 53</span> 
<span style="color: #008080;"> 54</span> <span style="color: #008000;">    public function prefixValue($prefix){
</span><span style="color: #008080;"> 55</span> <span style="color: #008000;">        return "{$prefix}".'ConcreteClass3';
</span><span style="color: #008080;"> 56</span> <span style="color: #008000;">    }
</span><span style="color: #008080;"> 57</span> <span style="color: #008000;">}
</span><span style="color: #008080;"> 58</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;"> 59</span> 
<span style="color: #008080;"> 60</span> 
<span style="color: #008080;"> 61</span> <span style="color: #800080;">$class1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ConcreteClass1;
</span><span style="color: #008080;"> 62</span> <span style="color: #800080;">$class1</span>-><span style="color: #000000;">printOut();
</span><span style="color: #008080;"> 63</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$class1</span>->prefixValue('FOO_').'<br>'<span style="color: #000000;">;
</span><span style="color: #008080;"> 64</span> 
<span style="color: #008080;"> 65</span> <span style="color: #800080;">$class2</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ConcreteClass2;
</span><span style="color: #008080;"> 66</span> <span style="color: #800080;">$class2</span>-><span style="color: #000000;">printOut();
</span><span style="color: #008080;"> 67</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$class2</span>->prefixValue('FOO_').'<br>'<span style="color: #000000;">;
</span><span style="color: #008080;"> 68</span> 
<span style="color: #008080;"> 69</span>  
<span style="color: #008080;"> 70</span> 
<span style="color: #008080;"> 71</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> AbstractClassB
</span><span style="color: #008080;"> 72</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 73</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> Our abstract method only needs to define the required arguments
</span><span style="color: #008080;"> 74</span> <span style="color: #008000;">    // 我们的抽象方法仅需要定义需要的参数</span>
<span style="color: #008080;"> 75</span>     <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> prefixNameB(<span style="color: #800080;">$name</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 76</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 77</span> 
<span style="color: #008080;"> 78</span> <span style="color: #0000ff;">class</span> ConcreteClassB <span style="color: #0000ff;">extends</span><span style="color: #000000;"> AbstractClassB
</span><span style="color: #008080;"> 79</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 80</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> Our child class may define optional arguments not in the parent's signature
</span><span style="color: #008080;"> 81</span> <span style="color: #008000;">    // 我们的子类可以定义父类签名中不存在的可选参数</span>
<span style="color: #008080;"> 82</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> prefixNameB(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$separator</span> = '.'<span style="color: #000000;">){
</span><span style="color: #008080;"> 83</span>         <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$name</span> == 'Pacman'<span style="color: #000000;">) {
</span><span style="color: #008080;"> 84</span>             <span style="color: #800080;">$prefix</span> = 'Mr'<span style="color: #000000;">;
</span><span style="color: #008080;"> 85</span>         } <span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$name</span> == 'Pacwoman'<span style="color: #000000;">) {
</span><span style="color: #008080;"> 86</span>             <span style="color: #800080;">$prefix</span> = 'Mrs'<span style="color: #000000;">;
</span><span style="color: #008080;"> 87</span>         } <span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;"> 88</span>             <span style="color: #800080;">$prefix</span> = ''<span style="color: #000000;">;
</span><span style="color: #008080;"> 89</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 90</span>         <span style="color: #0000ff;">return</span> "{<span style="color: #800080;">$prefix</span>}{<span style="color: #800080;">$separator</span>} {<span style="color: #800080;">$name</span>}"<span style="color: #000000;">;
</span><span style="color: #008080;"> 91</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 92</span> <span style="color: #000000;">}
</span><span style="color: #008080;"> 93</span> 
<span style="color: #008080;"> 94</span> <span style="color: #800080;">$classB</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ConcreteClassB;
</span><span style="color: #008080;"> 95</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$classB</span>->prefixNameB('Pacman'), '<br>'<span style="color: #000000;">;
</span><span style="color: #008080;"> 96</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$classB</span>->prefixNameB('Pacwoman'), '<br>'<span style="color: #000000;">;
</span><span style="color: #008080;"> 97</span> 
<span style="color: #008080;"> 98</span> 
<span style="color: #008080;"> 99</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;">100</span> <span style="color: #008000;">Object Interfaces 
</span><span style="color: #008080;">101</span> <span style="color: #008000;">Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. 
</span><span style="color: #008080;">102</span> <span style="color: #008000;">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 contents defined. 
</span><span style="color: #008080;">103</span> <span style="color: #008000;">All methods declared in an interface must be public; this is the nature of an interface. 
</span><span style="color: #008080;">104</span> <span style="color: #008000;">对象接口 
</span><span style="color: #008080;">105</span> <span style="color: #008000;">使用接口(interface),可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。 
</span><span style="color: #008080;">106</span> <span style="color: #008000;">接口是通过 interface 关键字来定义的,就像定义一个标准的类一样,但其中定义所有的方法都是空的。 
</span><span style="color: #008080;">107</span> <span style="color: #008000;">接口中定义的所有方法都必须是公有,这是接口的特性。 
</span><span style="color: #008080;">108</span> 
<span style="color: #008080;">109</span> 
<span style="color: #008080;">110</span> 
<span style="color: #008080;">111</span> 
<span style="color: #008080;">112</span> <span style="color: #008000;">implements 
</span><span style="color: #008080;">113</span> <span style="color: #008000;">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. 
</span><span style="color: #008080;">114</span> <span style="color: #008000;">Note: 
</span><span style="color: #008080;">115</span> <span style="color: #008000;">Prior to PHP 5.3.9, a class could not implement two interfaces that specified a method with the same name, since it would cause ambiguity. More recent versions of PHP allow this as long as the duplicate methods have the same signature. 
</span><span style="color: #008080;">116</span> <span style="color: #008000;">Note: 
</span><span style="color: #008080;">117</span> <span style="color: #008000;">Interfaces can be extended like classes using the extends operator. 
</span><span style="color: #008080;">118</span> <span style="color: #008000;">Note: 
</span><span style="color: #008080;">119</span> <span style="color: #008000;">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. 
</span><span style="color: #008080;">120</span> <span style="color: #008000;">Constants 
</span><span style="color: #008080;">121</span> <span style="color: #008000;">It's possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits them. 
</span><span style="color: #008080;">122</span> <span style="color: #008000;">实现(implements) 
</span><span style="color: #008080;">123</span> <span style="color: #008000;">要实现一个接口,使用 implements 操作符。类中必须实现接口中定义的所有方法,否则会报一个致命错误。类可以实现多个接口,用逗号来分隔多个接口的名称。 
</span><span style="color: #008080;">124</span> <span style="color: #008000;">Note: 
</span><span style="color: #008080;">125</span> <span style="color: #008000;">实现多个接口时,接口中的方法不能有重名。 
</span><span style="color: #008080;">126</span> <span style="color: #008000;">Note: 
</span><span style="color: #008080;">127</span> <span style="color: #008000;">接口也可以继承,通过使用 extends 操作符。 
</span><span style="color: #008080;">128</span> <span style="color: #008000;">Note: 
</span><span style="color: #008080;">129</span> <span style="color: #008000;">类要实现接口,必须使用和接口中所定义的方法完全一致的方式。否则会导致致命错误。 
</span><span style="color: #008080;">130</span> <span style="color: #008000;">常量 
</span><span style="color: #008080;">131</span> <span style="color: #008000;">接口中也可以定义常量。接口常量和类常量的使用完全相同,但是不能被子类或子接口所覆盖。 
</span><span style="color: #008080;">132</span> 
<span style="color: #008080;">133</span> 
<span style="color: #008080;">134</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;">135</span> 
<span style="color: #008080;">136</span> <span style="color: #008000;">//</span><span style="color: #008000;"> Declare the interface 'iTemplate'</span>
<span style="color: #008080;">137</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> iTemplate
</span><span style="color: #008080;">138</span> <span style="color: #000000;">{
</span><span style="color: #008080;">139</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setVariable(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$var</span><span style="color: #000000;">);
</span><span style="color: #008080;">140</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getHtml(<span style="color: #800080;">$template</span><span style="color: #000000;">);
</span><span style="color: #008080;">141</span> <span style="color: #000000;">}
</span><span style="color: #008080;">142</span> 
<span style="color: #008080;">143</span> <span style="color: #008000;">//</span><span style="color: #008000;"> Implement the interface
</span><span style="color: #008080;">144</span> <span style="color: #008000;">// This will work</span>
<span style="color: #008080;">145</span> 
<span style="color: #008080;">146</span> <span style="color: #0000ff;">class</span> Template <span style="color: #0000ff;">implements</span><span style="color: #000000;"> iTemplate
</span><span style="color: #008080;">147</span> <span style="color: #000000;">{
</span><span style="color: #008080;">148</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$vars</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
</span><span style="color: #008080;">149</span> 
<span style="color: #008080;">150</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setVariable(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$var</span><span style="color: #000000;">)
</span><span style="color: #008080;">151</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">152</span>         <span style="color: #800080;">$this</span>->vars[<span style="color: #800080;">$name</span>] = <span style="color: #800080;">$var</span><span style="color: #000000;">;
</span><span style="color: #008080;">153</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">154</span> 
<span style="color: #008080;">155</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getHtml(<span style="color: #800080;">$template</span><span style="color: #000000;">)
</span><span style="color: #008080;">156</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">157</span>         <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$this</span>->vars <span style="color: #0000ff;">as</span> <span style="color: #800080;">$name</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {
</span><span style="color: #008080;">158</span>             <span style="color: #800080;">$template</span> = <span style="color: #008080;">str_replace</span>('{'.<span style="color: #800080;">$name</span>.'}', <span style="color: #800080;">$value</span>, <span style="color: #800080;">$template</span><span style="color: #000000;">);
</span><span style="color: #008080;">159</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">160</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$template</span><span style="color: #000000;">;
</span><span style="color: #008080;">161</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">162</span> <span style="color: #000000;">}
</span><span style="color: #008080;">163</span> 
<span style="color: #008080;">164</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;">165</span> <span style="color: #008000;">class BadTemplate implements iTemplate
</span><span style="color: #008080;">166</span> <span style="color: #008000;">{
</span><span style="color: #008080;">167</span> <span style="color: #008000;">    private $var = array();
</span><span style="color: #008080;">168</span> <span style="color: #008000;">    public function setVariable($name, $var)
</span><span style="color: #008080;">169</span> <span style="color: #008000;">    {
</span><span style="color: #008080;">170</span> <span style="color: #008000;">        $this->vars[$name] = $var;
</span><span style="color: #008080;">171</span> <span style="color: #008000;">    }
</span><span style="color: #008080;">172</span> <span style="color: #008000;">}
</span><span style="color: #008080;">173</span> <span style="color: #008000;">Fatal error: Class BadTemplate contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (iTemplate::getHtml) 
</span><span style="color: #008080;">174</span> 
<span style="color: #008080;">175</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;">176</span> 
<span style="color: #008080;">177</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;">178</span> <span style="color: #008000;">class BadTemplate implements iTemplate
</span><span style="color: #008080;">179</span> <span style="color: #008000;">{
</span><span style="color: #008080;">180</span> <span style="color: #008000;">    private $vars = array();
</span><span style="color: #008080;">181</span> 
<span style="color: #008080;">182</span> <span style="color: #008000;">    public function setVariable($name, $var,$echo)
</span><span style="color: #008080;">183</span> <span style="color: #008000;">    {
</span><span style="color: #008080;">184</span> <span style="color: #008000;">        //Fatal error: Declaration of BadTemplate::setVariable() must be compatible with iTemplate::setVariable($name, $var)
</span><span style="color: #008080;">185</span> 
<span style="color: #008080;">186</span> <span style="color: #008000;">        $this->vars[$name] = $var;
</span><span style="color: #008080;">187</span> <span style="color: #008000;">        echo $echo;
</span><span style="color: #008080;">188</span> <span style="color: #008000;">    }
</span><span style="color: #008080;">189</span> 
<span style="color: #008080;">190</span> <span style="color: #008000;">    public function getHtml($template)
</span><span style="color: #008080;">191</span> <span style="color: #008000;">    {
</span><span style="color: #008080;">192</span> <span style="color: #008000;">        foreach ($this->vars as $name => $value) {
</span><span style="color: #008080;">193</span> <span style="color: #008000;">            $template = str_replace('{'.$name.'}', $value, $template);
</span><span style="color: #008080;">194</span> <span style="color: #008000;">        }
</span><span style="color: #008080;">195</span> <span style="color: #008000;">        return $template;
</span><span style="color: #008080;">196</span> <span style="color: #008000;">    }
</span><span style="color: #008080;">197</span> <span style="color: #008000;">}
</span><span style="color: #008080;">198</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;">199</span> 
<span style="color: #008080;">200</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;">201</span> <span style="color: #008000;">class BadTemplate implements iTemplate
</span><span style="color: #008080;">202</span> <span style="color: #008000;">{
</span><span style="color: #008080;">203</span> <span style="color: #008000;">    private $vars = array();
</span><span style="color: #008080;">204</span> 
<span style="color: #008080;">205</span> <span style="color: #008000;">    // Fatal error: Access level to BadTemplate::setVariable() must be public (as in class iTemplate)
</span><span style="color: #008080;">206</span> 
<span style="color: #008080;">207</span> <span style="color: #008000;">    protected function setVariable($name, $var)
</span><span style="color: #008080;">208</span> <span style="color: #008000;">    {
</span><span style="color: #008080;">209</span>         
<span style="color: #008080;">210</span> <span style="color: #008000;">        $this->vars[$name] = $var;
</span><span style="color: #008080;">211</span> <span style="color: #008000;">    }
</span><span style="color: #008080;">212</span> 
<span style="color: #008080;">213</span> <span style="color: #008000;">    public function getHtml($template)
</span><span style="color: #008080;">214</span> <span style="color: #008000;">    {
</span><span style="color: #008080;">215</span> <span style="color: #008000;">        foreach ($this->vars as $name => $value) {
</span><span style="color: #008080;">216</span> <span style="color: #008000;">            $template = str_replace('{'.$name.'}', $value, $template);
</span><span style="color: #008080;">217</span> <span style="color: #008000;">        }
</span><span style="color: #008080;">218</span> <span style="color: #008000;">        return $template;
</span><span style="color: #008080;">219</span> <span style="color: #008000;">    }
</span><span style="color: #008080;">220</span> <span style="color: #008000;">}
</span><span style="color: #008080;">221</span> 
<span style="color: #008080;">222</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;">223</span> 
<span style="color: #008080;">224</span> 
<span style="color: #008080;">225</span> 
<span style="color: #008080;">226</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> a
</span><span style="color: #008080;">227</span> <span style="color: #000000;">{
</span><span style="color: #008080;">228</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> foo();
</span><span style="color: #008080;">229</span> <span style="color: #000000;">}
</span><span style="color: #008080;">230</span> 
<span style="color: #008080;">231</span> <span style="color: #0000ff;">interface</span> b <span style="color: #0000ff;">extends</span><span style="color: #000000;"> a 
</span><span style="color: #008080;">232</span> <span style="color: #000000;">{
</span><span style="color: #008080;">233</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> baz(Baz <span style="color: #800080;">$baz</span><span style="color: #000000;">);
</span><span style="color: #008080;">234</span> <span style="color: #000000;">}
</span><span style="color: #008080;">235</span> 
<span style="color: #008080;">236</span> <span style="color: #0000ff;">class</span> c <span style="color: #0000ff;">implements</span><span style="color: #000000;"> b
</span><span style="color: #008080;">237</span> <span style="color: #000000;">{
</span><span style="color: #008080;">238</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> foo()
</span><span style="color: #008080;">239</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">240</span> 
<span style="color: #008080;">241</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">242</span> 
<span style="color: #008080;">243</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> baz(Baz <span style="color: #800080;">$baz</span><span style="color: #000000;">)
</span><span style="color: #008080;">244</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">245</span> 
<span style="color: #008080;">246</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">247</span> <span style="color: #000000;">}
</span><span style="color: #008080;">248</span> 
<span style="color: #008080;">249</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;">250</span> <span style="color: #008000;">Fatal error: Declaration of d::baz() must be compatible with b::baz(Baz $baz)
</span><span style="color: #008080;">251</span> 
<span style="color: #008080;">252</span> <span style="color: #008000;">class d implements b
</span><span style="color: #008080;">253</span> <span style="color: #008000;">{
</span><span style="color: #008080;">254</span> <span style="color: #008000;">    public function foo()
</span><span style="color: #008080;">255</span> <span style="color: #008000;">    {
</span><span style="color: #008080;">256</span> 
<span style="color: #008080;">257</span> <span style="color: #008000;">    }
</span><span style="color: #008080;">258</span> 
<span style="color: #008080;">259</span> <span style="color: #008000;">    public function baz(Foo $foo)
</span><span style="color: #008080;">260</span> <span style="color: #008000;">    {
</span><span style="color: #008080;">261</span> 
<span style="color: #008080;">262</span> <span style="color: #008000;">    }
</span><span style="color: #008080;">263</span> <span style="color: #008000;">}
</span><span style="color: #008080;">264</span> <span style="color: #008000;">*/</span>
<span style="color: #008080;">265</span> 
<span style="color: #008080;">266</span> 
<span style="color: #008080;">267</span> <span style="color: #008000;">//</span><span style="color: #008000;">Multiple interface inheritance 继承多个接口</span>
<span style="color: #008080;">268</span> 
<span style="color: #008080;">269</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> a1 
</span><span style="color: #008080;">270</span> <span style="color: #000000;">{
</span><span style="color: #008080;">271</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> foo();
</span><span style="color: #008080;">272</span> <span style="color: #000000;">}
</span><span style="color: #008080;">273</span> 
<span style="color: #008080;">274</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> b1
</span><span style="color: #008080;">275</span> <span style="color: #000000;">{
</span><span style="color: #008080;">276</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> bar();
</span><span style="color: #008080;">277</span> <span style="color: #000000;">}
</span><span style="color: #008080;">278</span> 
<span style="color: #008080;">279</span> <span style="color: #0000ff;">interface</span> c1 <span style="color: #0000ff;">extends</span> a1,<span style="color: #000000;"> b1
</span><span style="color: #008080;">280</span> <span style="color: #000000;">{
</span><span style="color: #008080;">281</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> baz();
</span><span style="color: #008080;">282</span> <span style="color: #000000;">}
</span><span style="color: #008080;">283</span> 
<span style="color: #008080;">284</span> <span style="color: #0000ff;">class</span> d1 <span style="color: #0000ff;">implements</span><span style="color: #000000;"> c1
</span><span style="color: #008080;">285</span> <span style="color: #000000;">{
</span><span style="color: #008080;">286</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> foo()
</span><span style="color: #008080;">287</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">288</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">289</span> 
<span style="color: #008080;">290</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> bar()
</span><span style="color: #008080;">291</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">292</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">293</span> 
<span style="color: #008080;">294</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> baz()
</span><span style="color: #008080;">295</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">296</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">297</span> <span style="color: #000000;">}
</span><span style="color: #008080;">298</span> 
<span style="color: #008080;">299</span> <span style="color: #008000;">//</span><span style="color: #008000;">Interfaces with constants 使用接口常量</span>
<span style="color: #008080;">300</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> a2
</span><span style="color: #008080;">301</span> <span style="color: #000000;">{
</span><span style="color: #008080;">302</span>     <span style="color: #0000ff;">const</span> b2 = 'Interface constant'<span style="color: #000000;">;
</span><span style="color: #008080;">303</span> <span style="color: #000000;">}
</span><span style="color: #008080;">304</span> 
<span style="color: #008080;">305</span> <span style="color: #0000ff;">echo</span> a2::<span style="color: #000000;">b2;
</span><span style="color: #008080;">306</span> 
<span style="color: #008080;">307</span> <span style="color: #008000;">/*</span>
<span style="color: #008080;">308</span> <span style="color: #008000;"> Fatal error: Cannot inherit previously-inherited or override constant b2 from interface a2 
</span><span style="color: #008080;">309</span> <span style="color: #008000;"> 错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。
</span><span style="color: #008080;">310</span> 
<span style="color: #008080;">311</span> <span style="color: #008000;">class c2 implements a2
</span><span style="color: #008080;">312</span> <span style="color: #008000;">{
</span><span style="color: #008080;">313</span> <span style="color: #008000;">    const  b2 ='Class constant';
</span><span style="color: #008080;">314</span> <span style="color: #008000;">}
</span><span style="color: #008080;">315</span> 
<span style="color: #008080;">316</span> <span style="color: #008000;">*/</span>
Salin selepas log masuk

 http://php.net/

 

小结:

0-子类需定义抽象类所有方法,方法参数个数可以添加,访问控制同或弱,而对象接口的实现也需要实现全部方法,但是参数个数不可更改,且访问控制必须public。

发问:

0-框架中的实例?

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan