Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of object-oriented characteristics and examples of php

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "php object-oriented features and examples", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the object-oriented features and examples of php".

Public public: this class, subclasses, and external objects can all be called

Protected protected: subclass of this class, can be executed, external objects cannot be called

Private proprietary: can only be executed by this class, and neither subclasses nor external objects can be called

Three characteristics of object-oriented programming

1) closeness

Closeness can also be called information hiding. Is to separate the use and implementation of a class, leaving only a limited number of interfaces (methods) connected with the outside world. For developers who use this class, they only need to know how the class is used, regardless of how the class is implemented. This allows developers to better focus on other things, while avoiding the inconvenience of interdependence between programs.

2) inheritance

Inheritance is that derived classes (subclasses) automatically inherit properties and methods from one or more base classes (parent classes) and can override or add new properties and methods. Inheriting this feature simplifies the creation of objects and classes and increases the reusability of the code. Inheritance single inheritance and multiple inheritance, PHP supports single inheritance, that is, a subclass has one and only one parent class.

3) Polymorphism

Polymorphism refers to different objects of the same class, and different results can be obtained by using the same method. This technique is called polymorphism. Polymorphism enhances the flexibility and reusability of the software.

Definition of class

A class can contain its own constants, variables (called properties), and functions (called methods).

Like many object-oriented languages, PHP defines classes by adding class names to the class keyword. The format of the class is as follows:

The copy code is as follows:

Definition: birds of a feather flock together to group objects with similar characteristics into a class that defines the same properties and methods that these similar objects have. A class is a description of a similar object, called the definition of a class, and is the blueprint or prototype of this kind of object.

An object of a class is called an instance of a class. To create an instance of a class, you must use the new keyword.

001ZpQGYty6MeYnSNUh35&690

The copy code is as follows:

Member method

Functions in a class are called member methods. The only difference between a function and a member method is that the function implements an independent function, while the member method is a behavior in the implementation class and is part of the class.

Let's extend the above myobject class by adding a member method to it, as follows:

The copy code is as follows:

The function of this method is to output the commodity name, which is passed in through the parameters of the method.

A class is an abstract description, a collection of objects that are functionally similar. If you want to use the methods and variables in the class, you must first implement it on an entity, that is, the object.

Quasi constant

Since there are variables, of course there will be constants. A constant is a quantity that does not change. It is a constant. A well-known constant is pi Pi. Define constants using keywords such as const:

ConstPI=3.14159

Constructor function

PHP 5 allows developers to define a method in a class as a constructor. Classes with constructors call this method every time a new object is created, so it is ideal for doing some initialization work before using the object.

The copy code is as follows:

Destructor function

The copy code is as follows:

The destructor executes when all references to an object are deleted or when the object is explicitly destroyed.

Reference to an object

The copy code is as follows:

At this point, I believe you have a deeper understanding of the "object-oriented features and examples of php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report