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

The role of keywords and Magic methods commonly used in PHP object-oriented

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "the role of keywords and magic methods commonly used in PHP object-oriented". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Keywords commonly used in PHP object-oriented

Final

1.final cannot modify member properties (this keyword is not used for constants in classes)

2.final can only modify classes and methods

Function:

Classes decorated with final cannot be inherited by subclasses

Methods modified with final cannot be overridden by subclasses

Used to restrict classes from being inherited, and methods not overridden to use final

Static (static keyword)

1. Using static, you can modify member properties and member methods, but not classes

two。 Member attributes modified with static can be shared by all objects of the same class

3. Static data is stored in a data segment in memory (initialize static segment)

4. Static data is allocated to memory when the class is first loaded, and when the class is used later, it is obtained directly from the data segment.

5. What is a class being loaded? As long as this class is used in the program (this class name appears)

6. Static methods (static-decorated methods) that cannot access non-static members (static members can be accessed in non-static methods)

Because non-static members must be accessed by objects, $this is used to access internal members, and static methods do not use object calls, so there is no object. $this cannot represent objects, and non-static members must also use objects.

If you determine that non-static members are not used in a method, you can declare the method as static

Note: static members should use the class name to access, do not create objects, do not use object access

Class name:: static member

If you use static members in a class, you can use self to represent this class

Const

1. It can only modify member properties.

two。 Class declares constant properties using const

3. Access in the same way as the static static member property (using the class name outside the class:: constant using the self:: constant inside the class)

4. Constants must be given initial values when they are declared.

_ _ toString ()

Automatically called when you directly output an object reference, which is the quickest way to quickly get a string representation

_ _ clone ()

Clone objects using clone () processing

Original (original object)

A copy (a copied object)

_ _ clone () is the method that is called automatically when the object is cloned

As soon as an object is created, it must have an initialization action, similar to the constructor _ _ constuct.

The $this keyword in the _ _ clone () method represents the object of the copy, and $that represents the original object.

_ _ autoload ()

Note: other magic methods are added to the class, which is the only one that is not added to the class

As long as a class is used in the page, as long as the class name is used, the class name will be automatically passed to this parameter.

Files in test

One.class.php

Two.class.php

Three.class.php

Object serialization (serialization): converts an object to a binary string (objects are stored in memory and are easy to release)

Usage time:

1. When objects are stored in a database or file for a long time

two。 When transferring objects in multiple PHP files

The parameter serialize (); is an object, and what is returned is the serialized binary string

The unserialize (); parameter is the binary string of the object, and the newly generated object is returned.

_ _ sleep ()

Is a method that is called during serialization

Function: an object can be partially serialized

As long as an array is returned in this method, several member properties in the array are serialized. If this method is not added, all members are serialized.

_ _ wakeup ()

Is a method that is called during deserialization

It is also the process of the rebirth of the object.

Read.php

Write.php

This is the end of the content of "keywords and Magic methods commonly used in PHP object-oriented". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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