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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

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.

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.

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.

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.

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

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

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 abstract classes and interfaces in PHP What is the difference between abstract classes and interfaces in PHP Mar 10, 2021 pm 05:51 PM

Differences: 1. Abstract classes can have attributes and ordinary methods, but interfaces cannot; 2. There may not be abstract methods in abstract classes, but there must be "abstract" methods in interfaces; 3. There are differences in syntax; 4. Abstract classes Use the abstract keyword to declare before a class, and a class is declared as a class. The interface is declared with interface, but cannot be declared with class; 5. The abstract method of an abstract class must be declared with abstract, but interfaces are not required; 6. Abstract classes and interfaces implement detailed methods in different ways.

See all articles