<?<span php </span><span /*</span><span * * php访问控制器的例子 *edit by www.jbxue.com </span><span */</span> <span class</span><span Foo { </span><span private</span> <span $name</span> = 'hdj'<span ; </span><span public</span> <span function</span><span getName(){ </span><span return</span> <span $this</span>-><span name; } } </span><span class</span> Bar <span extends</span><span Foo { </span><span public</span> <span $name</span> = 'deeka'<span ; } </span><span $bar</span> = <span new</span><span Bar; </span><span var_dump</span>(<span $bar</span>-><span name); </span><span var_dump</span>(<span $bar</span>-><span getName()); </span>?>
对属性或方法的访问控制,是通过在前面添加关键字 public、protected 或 private 来实现的。由 public 所定义的类成员可以在任何地方被访问;由 protected 所定义的类成员则可以被其所在类的子类和父类访问(当然,该成员所在的类也可以访问);而由 private 定义的类成员则只能被其所在类访问。
<span 1</span> <?<span php </span><span 2</span> <span /*</span><span * </span><span 3</span> <span * php访问控制 与 运算符优先级 </span><span 4</span> <span * by www.jbxue.com </span><span 5</span> <span */</span> <span 6</span> <span $a</span> = 3<span ; </span><span 7</span> <span $b</span> = 6<span ; </span><span 8</span> <span if</span>(<span $a</span> = 5 || <span $b</span> = 7<span ){ </span><span 9</span> <span echo</span> <span $b</span>.'<br />'<span ; </span><span 10</span> <span $a</span>++<span ; </span><span 11</span> <span $b</span>++<span ; </span><span 12</span> <span } </span><span 13</span> <span var_dump</span>(<span $a</span>, <span $b</span><span ); </span><span 14</span> <span echo</span> '<br /> $a = (5 || $b = 7)'<span ; </span><span 15</span> <span echo</span> '<hr />'<span ; </span><span 16</span> <span $a</span> = 3<span ; </span><span 17</span> <span $b</span> = 6<span ; </span><span 18</span> <span $c</span> = 1<span ; </span><span 19</span> <span if</span>(<span $a</span> = 5 || <span $b</span> = 7 && <span $c</span> = 10<span ){ </span><span 20</span> <span $a</span>++<span ; </span><span 21</span> <span $b</span>++<span ; </span><span 22</span> <span } </span><span 23</span> <span var_dump</span>(<span $a</span>, <span $b</span>,<span $c</span><span ); </span><span 24</span> <span echo</span> '<br /> && 比 || 高'<span ; </span><span 25</span> <span echo</span> '<hr />'<span ; </span><span 26</span> <span $a</span> = 3<span ; </span><span 27</span> <span $b</span> = 6<span ; </span><span 28</span> <span $c</span> = 1<span ; </span><span 29</span> <span if</span>(<span $a</span> = 0 || <span $b</span> = 7 && <span $c</span> = 10<span ){ </span><span 30</span> <span $a</span>++<span ; </span><span 31</span> <span $b</span>++<span ; </span><span 32</span> <span } </span><span 33</span> <span var_dump</span>(<span $a</span>, <span $b</span>,<span $c</span><span ); </span><span 34</span> <span echo</span> '<br /> '<span ; </span><span 35</span> <span echo</span> '<hr />'<span ; </span><span 36</span> <span class</span><span Foo { </span><span 37</span> <span private</span> <span $name</span> = 'hdj'<span ; </span><span 38</span> <span public</span> <span function</span><span getName() { </span><span 39</span> <span return</span> <span $this</span>-><span name; </span><span 40</span> <span } </span><span 41</span> <span } </span><span 42</span> <span 43</span> <span class</span> Bar <span extends</span><span Foo { </span><span 44</span> <span public</span> <span $name</span> = 'deeka'<span ; </span><span 45</span> <span } </span><span 46</span> <span 47</span> <span $bar</span> = <span new</span><span Bar; </span><span 48</span> <span var_dump</span>(<span $bar</span>-><span name); </span><span 49</span> <span var_dump</span>(<span $bar</span>-><span getName()); </span><span 50</span> ?>