Home > Backend Development > PHP Tutorial > PHP INCLUDE statement contains a series of different files_PHP tutorial

PHP INCLUDE statement contains a series of different files_PHP tutorial

WBOY
Release: 2016-07-15 13:35:10
Original
1192 people have browsed it

We have summarized for everyone

Every time an INCLUDE is encountered, the PHP INCLUDE statement includes the specified file. So you can use the INCLUDE statement in a loop structure to include a series of different files.

  1. $files = array('first.inc', '
    second.inc', 'third.inc');
  2. for ($i = 0; $i items[$artnr]
    += $num ;
  3. }
  4. // Take $num articles of $artnr
    out of the cart
  5. function remove_item($artnr, $num) {
  6. if ($this->items[$artnr] >$num) {
  7. $this->items[$artnr] -= $num;
  8. return true;
  9. } else { return false;
  10. }
  11. } >
  12. ?>
  13. The above PHP INCLUDE statement defines a Cart Class, which contains an associative array and two functions for adding and removing items from the cart. The class is a primitive model of the actual variable. You use the new operator to create a variable of the required type. >This creates an object of Cart class $cart. The function add_item() of this object is called to add 1 to the 10th item. The class can be extended or derived from other classes. The resulting class has all the variables and functions of the base class and what you defined in the extension definition using the extends keyword.
  14. The PHP INCLUDE statement here defines a class named Named_Cart, which 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 the carts. In the named_cart variable you can still use the general cart function:


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

The constructor in a class is a function that is automatically called when you create a new variable of a certain class. A function in a class with the same name as the class is a constructor.
<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">cart</span><span> = </span><span class="attribute-value">new</span><span> Cart;   </span></span></li>
<li>
<span>$cart-</span><span class="tag">></span><span>add_item("10", 1); </span>
</li>
</ol>
Copy after login


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

<ol class="dp-xml">
<li class="alt"><span><span>class Named_Cart extends Cart {   </span></span></li>
<li><span>var $owner;   </span></li>
<li class="alt"><span>function set_owner($name) {   </span></li>
<li>
<span>$this-</span><span class="tag">></span><span class="attribute">owner</span><span> = $name;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>} </span></li>
</ol>
Copy after login

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">ncart</span><span> = </span><span class="attribute-value">new</span><span> Named_Cart; </span></span></li>
<li class="alt"><span><span>// Create a named cart   </span></span></li>
<li>
<span>$ncart-</span><span class="tag">></span><span>set_owner("kris"); </span>
</li>
<li><span>// Name that cart   </span></li>
<li class="alt">
<span>print $ncart-</span><span class="tag">></span><span>owner; </span>
</li>
<li class="alt"><span>// print the cart owners name   </span></li>
<li>
<span>$ncart-</span><span class="tag">></span><span>add_item("10", 1); </span>
</li>
<li><span>// (inherited functionality from cart) </span></li>
</ol>
Copy after login


http://www.bkjia.com/PHPjc/445940.html

<ol class="dp-xml">
<li class="alt"><span><span>class Auto_Cart extends Cart {   </span></span></li>
<li><span> function Auto_Cart() {   </span></li>
<li class="alt">
<span> $this-</span><span class="tag">></span><span>add_item("10", 1);   </span>
</li>
<li><span> }   </span></li>
<li class="alt"><span> } </span></li>
</ol>
Copy after login
www.bkjia.com

true
<ol class="dp-xml">
<li class="alt"><span><span>class Constructor_Cart {   </span></span></li>
<li>
<span>function Constructor_Cart<br>($</span><span class="attribute">item</span><span> = </span><span class="attribute-value">"10"</span><span>, $</span><span class="attribute">num</span><span> = </span><span class="attribute-value">1</span><span>) {   </span>
</li>
<li class="alt">
<span>$this-</span><span class="tag">></span><span>add_item($item, $num);   </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>}   </span></li>
<li><span> // Shop the same old boring stuff.   </span></li>
<li class="alt">
<span>$</span><span class="attribute">default_cart</span><span> = </span><span class="attribute-value">new</span><span> Constructor_Cart;   </span>
</li>
<li><span>// Shop for real...   </span></li>
<li class="alt">
<span>$</span><span class="attribute">different_cart</span><span> = </span><span class="attribute-value">new</span><span> <br>Constructor_Cart("20", 17);  </span>
</li>
</ol>
Copy after login

http: //www.bkjia.com/PHPjc/445940.html

TechArticle

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template