Abstract class php simple application of abstract class
All right, the parent class postParent is defined as abstract, which stipulates that subclasses must reimplement the buildHTML() method. This method does not have curly braces. If there are curly braces, an error will be reported regardless of whether there is content or not.
The more I look at it now, the more I feel that there is no need to use abstract classes in this code, and inheritance is useless. Well, there is nothing to say. . . . .
In addition, I separated mysql outside, so calling the method is very troublesome
1, instantiate readArticle first
2, mysql query, the parameters come from readArticle::getSQL();
3, return the mysql result resource to readArticle::fetchResult( $result );
4, readArticle::buildHTML(); returns HTML
If it is a list loop output, just call 3 and 4 repeatedly
Copy the code The code is as follows:
abstract class postParent
{
protected $querySQL;
public $fetchResult;
public $timeAgo; // eg : 2 days ago
abstract protected function buildHTML();
public function getSQL()
{
return $this->querySQL;
}
public function fetchResult( $result )
{
$this->fetchResult = mysql_fetch_assoc( $result );
}
public function error()
{}
}
class readArticle extends postParent
{
public function __construct ( $id )
{
$this->querySQL =<<
WHERE id = $id ORDER BY unixtime DESC;
eof;
}
public function buildHTML()
{
return <<
{$this->fetchResult['author']} at
{$this->fetchResult['text']}
eof;
}
}
The above introduces the simple application of abstract classes in PHP, including the content of abstract classes. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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

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.

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

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.

Tips for optimizing the performance of interfaces and abstract classes in Java: Avoid using default methods in interfaces and only use them when necessary. Minimize interface definition to include only necessary content. Implement as many abstract class methods as possible. Use the final modifier to prevent overriding by subclasses. Declare methods that should not be called as private.

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.
