国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
PHP object-oriented basics (interfaces, classes), PHP is oriented
Home Backend Development PHP Tutorial PHP object-oriented basics (interface, class), php-oriented_PHP tutorial

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

Jul 12, 2016 am 08:52 AM
abstract class

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>

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>;

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>

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>方法實(shí)例方法</span>
   self::<span>$static_value</span> ;<span>//</span><span>訪問靜態(tài)變量</span>
   <span>static</span>::<span>$static_value</span>;<span>//</span><span>同上</span>


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,因?yàn)閟elf本意其實(shí)指向parentclass的
     //如果你使用了self,那么將拋出異常,提示抽象類無法實(shí)例化
     //而static并不直接指向parentclass而是作用與包含類
     //</span>
<span>   }
 }

 </span><span>class</span> ChildClass <span>extends</span><span> ParentClass{
   </span><span>//
</span> }

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>) 當(dāng)訪問未定義的屬性時候該方法被調(diào)用
 __set(</span><span>$property</span>,<span>$value</span><span>)當(dāng)給未定義的屬性賦值時被調(diào)用
 </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不存在,那么將走_(dá)_setz最后給$Name賦值</span>
 <span>echo</span> <span>$obj</span>->Name ; <span>//</span><span> output: fz-visonme</span>

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>

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>

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>

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實(shí)現(xiàn)clone的控制
  //這里內(nèi)部同時對Account實(shí)現(xiàn)一次clone,這里就可以避免b例子中出現(xiàn)的問題</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>

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...
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Application of interfaces and abstract classes in design patterns in Java Application of interfaces and abstract classes in design patterns in Java May 01, 2024 pm 06:33 PM

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

Does golang have abstract classes? Does golang have abstract classes? Jan 06, 2023 pm 07:04 PM

Golang has no abstract classes. Golang is not an object-oriented (OOP) language. It has no concepts of classes, inheritance, and abstract classes. However, there are structures (structs) and interfaces (interfaces) in golang, which can be indirectly implemented through the combination of struct and interface. Abstract classes in object languages.

What is the difference between interfaces and abstract classes in PHP? What is the difference between interfaces and abstract classes in PHP? Jun 04, 2024 am 09:17 AM

Interfaces and abstract classes are used to create extensible PHP code, and there is the following key difference between them: Interfaces enforce through implementation, while abstract classes enforce through inheritance. Interfaces cannot contain concrete methods, while abstract classes can. A class can implement multiple interfaces, but can only inherit from one abstract class. Interfaces cannot be instantiated, but abstract classes can.

What is the difference between an abstract class and an interface in PHP? What is the difference between an abstract class and an interface in PHP? Apr 08, 2025 am 12:08 AM

The main difference between an abstract class and an interface is that an abstract class can contain the implementation of a method, while an interface can only define the signature of a method. 1. Abstract class is defined using abstract keyword, which can contain abstract and concrete methods, suitable for providing default implementations and shared code. 2. The interface is defined using the interface keyword, which only contains method signatures, which is suitable for defining behavioral norms and multiple inheritance.

An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes Apr 20, 2024 am 09:21 AM

Both functional interfaces and abstract classes are used for code reusability, but they are implemented in different ways: functional interfaces through reference functions, abstract classes through inheritance. Functional interfaces cannot be instantiated, but abstract classes can. Functional interfaces must implement all declared methods, while abstract classes can only implement some methods.

Inner class implementation of interfaces and abstract classes in Java Inner class implementation of interfaces and abstract classes in Java Apr 30, 2024 pm 02:03 PM

Java allows inner classes to be defined within interfaces and abstract classes, providing flexibility for code reuse and modularization. Inner classes in interfaces can implement specific functions, while inner classes in abstract classes can define general functions, and subclasses provide concrete implementations.

Java Interfaces and Abstract Classes: The Road to Programming Heaven Java Interfaces and Abstract Classes: The Road to Programming Heaven Mar 04, 2024 am 09:13 AM

Interface: An implementationless contract interface defines a set of method signatures in Java but does not provide any concrete implementation. It acts as a contract that forces classes that implement the interface to implement its specified methods. The methods in the interface are abstract methods and have no method body. Code example: publicinterfaceAnimal{voideat();voidsleep();} Abstract class: Partially implemented blueprint An abstract class is a parent class that provides a partial implementation that can be inherited by its subclasses. Unlike interfaces, abstract classes can contain concrete implementations and abstract methods. Abstract methods are declared with the abstract keyword and must be overridden by subclasses. Code example: publicabstractcla

Java interfaces and abstract classes: revealing the inner connection between them Java interfaces and abstract classes: revealing the inner connection between them Mar 04, 2024 am 09:34 AM

Interface Interface defines abstract methods and constants in Java. The methods in the interface are not implemented, but are provided by the class that implements the interface. The interface defines a contract that requires the implementation class to provide specified method implementations. Declare the interface: publicinterfaceExampleInterface{voiddoSomething();intgetSomething();} Abstract class An abstract class is a class that cannot be instantiated. It contains a mixture of abstract and non-abstract methods. Similar to interfaces, abstract methods in abstract classes are implemented by subclasses. However, abstract classes can also contain concrete methods, which provide default implementations. Declare abstract class: publicabstractcl

See all articles