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 INCLUDE statement in PHP

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the example analysis of INCLUDE sentence in PHP, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

The PHP INCLUDE statement contains the specified file each time an INCLUDE is encountered. So you can use the include statement in a loop structure to contain a series of different files.

$files = array ('first.inc','

Second.inc', 'third.inc')

For ($I = 0; $I items [$artnr]

+ = $num

}

/ / Take $num articles of $artnr

Out of the cart

Function remove_item ($artnr, $num) {

If ($this- > items [$artnr] > $num) {

$this- > items [$artnr]-= $num

Return true

} else {

Return false

}

}

}

? >

The above PHP INCLUDE statement defines a class called Cart, which includes an associative array and two functions to add and delete items from cart.

Class is the original model of the actual variable. You need to create a variable of the desired type through the new operator.

$cart = new Cart; $cart- > add_item ("10", 1)

This creates an object of the Cart class $cart. The object's function add_item () is called to add 1 to item 10.

Classes can be extended from other classes. An extended or derived class has all the variables and functions of the base class and what you define in the extended definition. This uses the extends keyword.

Class Named_Cart extends Cart {var $owner; function set_owner ($name) {$this- > owner = $name;}}

Here the PHP INCLUDE statement defines a class named Named_Cart that inherits all the variables and functions of the Cart class and adds a variable $owner and a function set_owner (). The variable of the named_cart class you created can now set the owner of carts. You can still use the normal cart function in the named_cart variable:

$ncart = new Named_Cart; / / Create a named cart $ncart- > set_owner ("kris"); / / Name that cart print $ncart- > owner; / / print the cart owners name $ncart- > add_item ("10", 1); / / (inherited functionality from cart)

The variable $this in the function means the current object. You need to use the form $this- > something to access all the variables or functions of the current object.

A constructor in a class is a function that is automatically called when you create a new variable of some kind. The function in the class that is the same as the class name is the constructor.

Class Auto_Cart extends Cart {function Auto_Cart () {$this- > add_item ("10", 1);}}

Here the PHP INCLUDE statement defines a class Auto_Cart, which adds a constructor to the Cart class that sets item 10 for variable initialization each time a new operation is performed. Constructors can also have parameters, which are optional, which makes them useful.

Class Constructor_Cart {

Function Constructor_Cart

($item = "10", $num = 1) {

$this- > add_item ($item, $num)

}

}

/ / Shop the same old boring stuff.

$default_cart = new Constructor_Cart

/ / Shop for real...

$different_cart = new

Constructor_Cart (20, 17)

Thank you for reading this article carefully. I hope the article "sample Analysis of INCLUDE sentences in PHP" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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