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

What are the differences between php4 and php5

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

Share

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

This article mainly introduces the relevant knowledge of "what is the difference between php4 and php5". The editor shows you the operation process through actual cases, the operation method is simple and fast, and it is practical. I hope this article "what is the difference between php4 and php5" can help you solve the problem.

Operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

What's the difference between php4 and php5?

Constructor and destructor

In PHP4, when a function has the same name as an object, this function becomes the constructor of that object, and there is no concept of destructor in PHP4.

In PHP5, the constructor is uniformly named _ _ construct, and the concept of destructor is introduced, which is uniformly named _ _ destruct.

Example 1: constructor and destructor

Class foo {undefinedvar $x _ function _ construct ($x) {undefined$this- > x = $x;} function display () {undefinedprint ($this- > x);} function _ _ destruct () {undefinedprint ("bye bye");}} $o1 = new foo (4); $o1-> display ();? >

In the above example, when you terminate the call to the foo class, its destructor will be called, and in the above example, "bye bye" will be output.

Reference to an object

As we all know, in PHP4, passing a variable to a function or method actually makes a copy of the variable, which means that you pass a copy of the variable to the function or method, unless you use the reference symbol "&" to declare that you want to make a reference, not a Copy. In PHP5, an object always exists as a reference, and the assignment operation in the object is also a reference operation.

Example 2: reference to an object

Class foo {undefinedvar $x setX function setX ($x) {undefined$this- > x = $x;} function getX () {undefinedreturn $this- > x;}} $o1 = new foo;$o1- > setX (4); $O2 = $o1X setX (5); if ($o1-> getX () = = $O2-> getX ()) print ("Oh my god!");? >

Cloning of objects

As mentioned above, what if I want to get a copy of an object when it is always called in the form of a reference? PHP5 provides a new function, which is the cloning of objects, and the syntax is _ _ clone.

Example 3: cloning of objects

Class foo {undefinedvar $x clone function setX ($x) {undefined$this- > x = $x;} function getX () {undefinedreturn $this- > x;}} $o1 = new foo;$o1- > setX (4); $O2 = $o1-> _ clone (); $o1-> setX (5); if ($o1-> getX ()! = $O2-> getX ()) print ("Copies are independant");? >

The method of object cloning exists in many other application languages, so you don't have to worry about its stability. :)

Private, public, and protected modes in objects

In PHP4, all methods and variables of an object are public, which means that you can manipulate any of them outside of an object. PHP5 introduces three new modes to control this access: public (Public), protected (Protected), and private (Private).

Common mode (Public): allows operational control outside the object.

Private mode (Private): only methods within this object are allowed to operate on it.

If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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