PHP Classes and objects

Source: Internet
Author: User
Tags php class

Look at an example

The code is as follows Copy Code

<?php
Class Cart
{
    var $items; //Shopping cart Items
   //Put $num $a Rtnr into the car
    function Add_item ($ARTNR, $num)
    {
         $this->items[$artnr] + = $num;
   }
   //Take $num $artnr out of the car
    function Remove_item ($ARTNR, $num)
 &nbs p;  {
        if ($this->items[$artnr] > $num) {
             $this->items[$artnr]-= $num;
            return true;
       } else {
             return false;
       }
   }
}

The above type defines a class called Class, which is a class consisting of an array of shopping carts and two items that are used to add and remove merchandise functions from a shopping cart.

Declares the class we want to see how the example uses

Create an object,

The code is as follows Copy Code

$obj 111cn_.net = new Cart;

Instance

The code is as follows Copy Code

<?php
Class session
{
Public Function __construct ()
{
Echo ' constructor execution ';
}
Public Function __destruct ()
{
echo ' destructor execution ';
}
}
$OBJ = new Session;
Unset ($OBJ);//Delete object, __destruct () will be invoked
Output
Constructor execution
/destructor Execution
?>

$obj 111cn_.net->add_item (1,2);

This is the property and method of the calling class.

The code is as follows Copy Code

<?php
Class session
{
Public $age = 20;
Public $sub = ' www.111cn.net ';
Public Function __clone ()
{
$this->sub=clone $this->sub;
}
}
Class Session 2
{
Public $value = 5;
}
$s = new session;
$s->sub=new session 2;
$s 2 = clone $s;
$s->sub-> $value = 10;
echo $s 2->sub-> $value;
?>

Invoke PHP Class Property and method instance two

The code is as follows Copy Code

<?php
Class session
{
Public $age = 20;
Public $sub = ' 111cn.net ';
Public Function __clone ()
{
$this->sub=clone $this->sub;
}
}
Class Session 2
{
Public $value = 5;
}
$s = new session;
$s->sub=new session 2;
$s 2 = clone $s;
$s->sub-> $value = 10;
echo $s 2->sub-> $value;
?>

This site original tutorial reproduced annotated source Www.111cn.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.