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

Example Analysis of authoritative programming in PHP5

2025-04-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you the example analysis of PHP5 authoritative programming. I hope you will get something after reading this article. Let's discuss it together.

In PHP4, instead of using _ _ construct () as the name of the constructor, you must define a method with the name of the class, just as in C++.

In PHP5, use the new unified constructor naming method: _ _ construct (). Of course, it is also possible to use the class name.

However, if you use both at the same time, the system will use the form _ _ construct () by default.

The copy code is as follows:

You cannot return a value in a constructor, so the most common way to generate an error from within the constructor is to throw an exception.

The code is as follows:

The copy code is as follows:

access control

Access protection of object properties is a key example of OOP

Public: can be accessed from anywhere

Protected: class members can be accessed by subclasses and parents of their class from methods inside the object

Private: class members can only be accessed by methods within the object of their class, not from members of inherited classes. Because private members are not inherited, it is entirely possible for two related classes to declare a private variable with the same name.

That is, both classes can only see their own private properties, and there is no relationship between private members.

Example:

The copy code is as follows:

Clone object

In PHP4, when you new an object, you return "the object itself"

In PHP5, when you new an object, "handle to the object" is returned.

This means that in PHP5, when an object instance ($obj1) is assigned to another variable ($obj2), both objects point to the same memory area.

For example:

The copy code is as follows:

Because $obj1 and $obj2 point to the same area of memory, using either object to modify the value of one of the member variables will affect the other object.

But there are times when we do need to make a copy of an object (two independent memory regions), and we can use the language command clone

Refer to the following example

The copy code is as follows:

Parent:: and self::

Self:: points to the current class and is usually used to access static members, methods, and constants

Parent:: points to the parent class, and it is often used to call the constructors and methods of the parent class, as well as to access the members and constants of the parent class

Note: you should use parent:: instead of a specific name of the parent class, because it makes it easy for you to change the hierarchy of your class.

Example:

The copy code is as follows:

Results:

Call the constructor of the parent class

Call the constructor of the subclass

Recommended use method 1, the reason has been mentioned above.

Instanceof instance

The copy code is as follows:

Note: _ _ CLASS__ is a special constant that stores the name of the current class

After reading this article, I believe you have some understanding of "sample Analysis of authoritative programming in PHP5". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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