PHP object-oriented basics (interface, class), php-oriented_PHP tutorial

WBOY
Release: 2016-07-12 08:52:37
Original
966 people have browsed it

PHP object-oriented basics (interfaces, classes), PHP is oriented

Introducing the basic knowledge of PHP object-oriented

1. Definition of interface interface, class definition class, class supports abstract and final modifiers, abstract is modified into abstract class, abstract class

Does not support direct instantiation, and final-modified classes/methods cannot be inherited/method overridden.
2. The interface is implemented through implements, and class inheritance extends

  <span>interface</span><span> IShape{
    </span><span>function</span><span> draw_core();
  };

  </span><span>class</span> PathShape <span>implements</span><span> IShape{
    </span><span>public</span> <span>function</span><span> draw_core(){}
  }

  </span><span>class</span> Rectangle <span>extends</span><span> PathShape{
    </span><span>public</span> <span>function</span><span> draw_core(){
      </span><span>//</span><span>overide draw_core</span>
<span>    }
  }</span>
Copy after login

3. Static variables and constants (static, const)
a. There is no need to add the dollar modifier $ in front of the constant declaration variable name, and static variables require
b. Both are accessed through classes, static variable methods Sometimes you need to add the $ dollar modifier before the variable name

   <span>class</span><span> MyClass{
     </span><span>const</span><span> M_CONST_VALUE;
     </span><span>static</span> <span>$M_STATIC_VALUE</span><span>;
   }

   MyClass</span>::<span>M_CONST_VALUE ;
   MyClass</span>::<span>$M_STATIC_VALUE</span>;
Copy after login

c. Access permission modifiers are not supported when declaring constants. Public cannot be added before const. Constants default to public.

   <span>const</span> M_CONST  ; <span>//</span><span>OK</span>
   <span>public</span> <span>const</span> M_CONST ; <span>//</span><span> throw exception</span>
Copy after login

4. Access non-static/constant variables and methods within a class through $this, access the parent class through parent, and access static variables and methods within a class through
self. Self essentially points to the class or through static Visit

   parent::method(); <span>//</span><span>父类方法</span>
   <span>$this</span>->method() ; <span>//</span><span>方法实例方法</span>
   self::<span>$static_value</span> ;<span>//</span><span>访问静态变量</span>
   <span>static</span>::<span>$static_value</span>;<span>//</span><span>同上</span>
Copy after login


5. The difference between static and self is that self refers to the parsing context, which is also the current class. Static refers to the class that is called
rather than the containing class. A typical example is a singleton

 <span>abstract</span> <span>class</span><span> ParentClass{
   </span><span>public</span> <span>static</span> <span>function</span><span> createInstance(){
     </span><span>return</span> <span>new</span> <span>static</span><span>(); 
     </span><span>//</span><span>这里不能使用self,因为self本意其实指向parentclass的
     //如果你使用了self,那么将抛出异常,提示抽象类无法实例化
     //而static并不直接指向parentclass而是作用与包含类
     //</span>
<span>   }
 }

 </span><span>class</span> ChildClass <span>extends</span><span> ParentClass{
   </span><span>//
</span> }
Copy after login

7. Use interceptors in classes. PHP interceptors include __get, __set, __inset, __unset, __call. Here we only focus on geth and set interceptors

 __get(<span>$property</span><span>) 当访问未定义的属性时候该方法被调用
 __set(</span><span>$property</span>,<span>$value</span><span>)当给未定义的属性赋值时被调用
 </span><span>class</span><span> MyClass{
    </span><span>public</span> <span>function</span> __get(<span>$property</span><span>){
       </span><span>echo</span> "Access __get"<span>;
       </span><span>if</span>(property_exists(<span>$this</span>,<span>$property</span><span>)){
          </span><span>return</span> <span>$this</span>-><span>$property</span><span>;
       }</span><span>else</span><span>{
         </span><span>return</span> "unknown"<span>;
       }
    }

    </span><span>public</span> <span>function</span> __set(<span>$property</span>,<span>$value</span><span>){
      </span><span>if</span>(!property_exists(<span>$this</span>,<span>$property</span><span>)){
       </span><span>$this</span>->Name = <span>$value</span>; <span>//</span><span>变量不存在就直接给$Name赋值</span>
<span>      }
    }
    
    </span><span>public</span> <span>$Name</span> = "visonme"<span>;
 };
 </span><span>//</span><span>访问</span>
 <span>$obj</span>  = <span>new</span><span> MyClass();
 </span><span>$obj</span>->Name ; <span>//</span><span>直接访问变量$Name</span>
 <span>$obj</span>->Password;<span>//</span><span>Password未定义,先访问__get最后输出unknown

 //-for __set</span>
 <span>$obj</span>->password = 'fz-visonme';<span>//</span><span>password不存在,那么将走__setz最后给$Name赋值</span>
 <span>echo</span> <span>$obj</span>->Name ; <span>//</span><span> output: fz-visonme</span>
Copy after login

8. Class constructor and destructor: __construct, __destruct. The constructor is called when instantiating an object and is mostly used for member variable initialization. Destructor is called when the class is destroyed and is mostly used for finishing work

<span>class</span><span> MyClass{
  </span><span>function</span><span> __construct(){}
  </span><span>function</span><span> __destruct(){}
}</span>
Copy after login

9. The object is copied through clone. The clone keyword uses the "value copy" method to generate a new object. The object copy itself is still copied by reference.

a. Simple type assignment

<span>class</span><span> MyClass{
  </span><span>public</span> <span>$ID</span><span>;
};

</span><span>$a</span> = <span>new</span><span> MyClass;
</span><span>$a</span>->ID = 199<span>;
</span><span>$b</span> = <span>clone</span> <span>$a</span><span>;  
</span><span>echo</span> <span>$b</span>->ID;   <span>//</span><span> output: 199</span>
Copy after login

b. Copy of containing objects

<span>class</span><span> Account{
  </span><span>public</span> <span>$RMB</span><span>;
};
</span><span>class</span><span> MyClass{
  </span><span>public</span> <span>$ID</span><span>;
  </span><span>public</span> <span>$AccountObj</span><span>;

  </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){
    </span><span>$this</span>->AccountObj = <span>$c</span><span>;
  }
};

</span><span>$a</span> = <span>new</span> MyClass(<span>new</span><span> Account());
</span><span>$a</span>->AccountObj->RMB= 199<span>;
</span><span>$b</span> = <span>clone</span> <span>$a</span><span>;
</span><span>echo</span> <span>$b</span>->AccountObj->RMB;   <span>//</span><span>output: 199</span>
<span>$a</span>->AccountObj->RMB = 100<span>;
</span><span>echo</span> <span>$b</span>->AccountObj->RMB;  <span>//</span><span>output: 100</span>
<span>
在clone后,</span><span>$a的AccountObj改变时候</span>,同时会影响到<span>$b</span>
Copy after login

This result is obviously not what we expect. What we expect is that ab is two independent objects without any correlation.

In order to solve this problem, we can implement __clone inside the class. When we call clone outside, the __clonef method of the class will be called internally, so we can achieve control of clone by overriding __clone. For example Modification of example b

<span>class</span><span> MyClass{
  </span><span>public</span> <span>$ID</span><span>;
  </span><span>public</span> <span>$AccountObj</span><span>;

  </span><span>public</span> <span>function</span> __construct(<span>$c</span><span>){
    </span><span>$this</span>->AccountObj = <span>$c</span><span>;
  }

  </span><span>//</span><span>__clone实现clone的控制
  //这里内部同时对Account实现一次clone,这里就可以避免b例子中出现的问题</span>
  <span>public</span> <span>function</span><span> __clone(){
    </span><span>$this</span>->ID = 0 ; <span>//</span><span>将ID置为0,如果你需要的话</span>
    <span>$this</span>->AccountObj = <span>clone</span> <span>$this</span>-><span>AccountObj;
  }
};</span>
Copy after login

We need to know about the __clone method. This method is called on the cloned object, not on the original object. For example, in the example b above

$b = clone $a; //Execution process: Basic copy object $a ---> $b executes __clone()


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1126840.htmlTechArticlePHP object-oriented basics (interface, class), php-oriented introduction to the basic knowledge of PHP object-oriented 1. Interface Define interface, class definition class, class supports abstract and final modifiers, abstract modification...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template