php classes and objects_PHP tutorial
php classes and objects
Object-oriented is the mainstream of programming today. R&D personnel may have some understanding of object-oriented, but some of the less commonly used ones may not be particularly clear. Sometimes it's also useful. Here are some tips.
1. Some knowledge about final keyword:
1. The final keyword as a method can be inherited by subclasses. As follows:
class A{ final function operation(){ echo 'a'; } } class B extends A{ } $a=new B(); $a->operation();
result :a
2. The final keyword cannot be inherited as a class, as follows:
Copy after login
( ! ) Fatal error: Class B may not inherit from final class (A) in D:wampwwwexambleindex19.php on line 9
3. The final keyword as a method cannot be overridden by subclasses, which means that subclasses cannot have the same method, as follows
class A{ final function operation(){ echo 'a'; } } class B extends A{ function operation(){ echo 'a'; } } $a=new B(); $a->operation();
There will be the following error:
( ! ) Fatal error: Cannot override final method A::operation() in D:wampwwwexambleindex19.php on line
|
---|
2. Implementation of multiple inheritance in PHP. The following example will have a fatal error in PHP.
class A{ public function operation(){ echo 'a'; } } class C{ public function oper(){ echo 'c'; } } class B extends A{ public function operation(){ echo 'a'; } } class B extends C{ public function operati(){ echo 'd'; } } $a=new B(); $a->operation();
( ! ) Fatal error: Cannot redeclare class B in D:wampwwwexambleindex19.php on line 24 |
---|
( ! ) Fatal error: Cannot redeclare class B in D:wampwwwexambleindex19.php on line 24 |
---|
This form of multiple inheritance is not allowed.
If you have to implement multiple inheritances, you can only achieve it through interfaces.
interface Displayable{ public function display(); } interface B{ public function show(); } class A implements Displayable,B{ public function display(){ echo 'a'; } public function show(){ echo 'b'; } } $ab=new A(); $ab->display(); $ab->show();
Note that the methods of the interface are all public. The methods of the interface only have methods and no method bodies. Subclasses override the methods of the interface. All methods of the interface must be rewritten in the subclass.
php classes and objects are object-oriented, which is the mainstream of programming today. For R&D personnel, they may be more interested in object-oriented. A few people have some knowledge, but some that are not commonly used may not be particularly clear...

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

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

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











JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

The difference between Java heap and stack and application scenario analysis require specific code examples. In Java programs, heap and stack are two commonly used data structures, and they assume different roles and functions in memory. Understanding the difference between heap and stack is crucial to writing efficient Java programs. First, let's take a look at the Java heap. The heap is an area used to store objects. All objects created in the program are stored in the heap. The heap is where memory is dynamically allocated and released while the program is running. It is not subject to any restrictions and can be automatically allocated and released as needed.
