


Let's talk about the basics of object-oriented programming in PHP (1)
This article mainly talks about the foundation of PHP object-oriented programming (1), which has certain learning value. Interested friends can learn about it.
The basis of process-oriented is a sentence of code, while the basis of object-oriented is objects, and objects are derived from instances of classes.
The definition of a class: a collection of things with the same attribute definitions and behavior.
A class is a collection of variables (variable attributes) and functions (class methods) that act on these variables. Attributes and methods are the basis of a class.
1. Class encapsulation
A class is a collection of variables and functions that act on variables, so creating a class cannot be separated from variables and functions.
Adding attributes to a class means adding new variables to the class, which can be defined using the public, protected, and private keywords. Variables modified with public can be accessed outside the class, while variables modified with protected and private cannot be accessed outside the class.
Adding a method to a class means adding a function to the class, and calling a class method means executing the function. To add methods to a class, just add a new function to the class.
If you want to reference the properties or methods of the class itself in the function, you must use the pseudo variable $this plus the name of the referenced property or method to achieve the function.
2. Class inheritance
Usually you need classes that have the same variables and functions as other existing classes.
A class that is extended or derived has all the variables and functions of the base class or parent class, and includes all defined parts of the derived class. At the same time, extended classes always rely on a single base class, that is, multiple inheritance is not supported.
Syntax: class Subclass extends Parent class
It should be noted that properties and methods modified with public and protected can be inherited by subclasses, while properties and methods modified with private cannot be inherited by subclasses.
3. Overloading of classes
The attributes or methods in a subclass sometimes have the same name as the attributes or methods in the parent class it inherits. At this time, the class overloading occurs. Overload.
Overloading of classes is actually the overloading of class attributes and class methods.
Of course, you can also access methods in the parent class in the subclass, but you need to use the two special keywords self and parent, which are used to access members or methods inside the class. .
$this is a pointer to an object instance, which is determined during instantiation;
self refers to a reference to the class itself, and generally self points to a static variable in the class. Format: self::static variable name
Parent is a reference to the parent class. Generally, parent is used to call the constructor of the parent class. .
4. Functions related to classes and objects in PHP
class_exists() function return type: Boolean value
Function: This function checks whether the class has been defined
Get_class_methods() function return type: array (all method names)
Function: This function returns an array composed of class method names
get_class_vars() function return type: array ( All public attributes of the class)
Function: This function will return an associative array composed of the default public attributes of the class, in the form: varname=>value
The get_class() function return type: character String
Function: This function will return the name of the class to which the object instance belongs
get_declared_classes() function return type: Array
Function: This function will return the name of the class specified by the current script An array of names of classes defined in .
Get_object_vars() function Return type: array
Function: This function returns an associative array composed of object attributes.
Get_parent_class() function Return type: String
Function: This function returns the parent class name of the object or class.
Return type of is_subclass_of() function: Boolean value
Function: Determine the relationship between the object and the class
Method_exists() function Return type: Boolean value
Function: This function checks whether the method of the class exists.
Related tutorials: PHP video tutorial
The above is the detailed content of Let's talk about the basics of object-oriented programming in PHP (1). For more information, please follow other related articles on the PHP Chinese website!

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
