PHP 4

[ PHP ]

(!!! Teleportom!!!)
: fopen, msql_num_fields, socket_write ...


13.

class
extends
Constructors
::
parent
-
__sleep __wakeup

class

, . :

<?php

class Cart
{
    var $items;  //   shopping cart

    //  $num  $artnr  cart

    function add_item ($artnr, $num)
    {
        $this->items[$artnr] += $num;
    }
   
    //  $num  $artnr  cart
 
    function remove_item ($artnr, $num)
    {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } else {
            return false;
        }   
    }
}
?>

Cart, /articles /cart cart.

!

PHP 4.

stdClass Zend- . stdClass.

__sleep __wakeup "" PHP-. , "" , . . .

PHP , __ . __ , - .

: PHP 4 var- . , , . (. ).

<?php
/*      PHP 4. */
class Cart
{
    var $todays_date = date("Y-m-d");
    var $name = $firstname;
    var $owner = 'Fred ' . 'Jones';
    var $items = array("VCR", "TV");
}

/*     . */
class Cart
{
    var $todays_date;
    var $name;
    var $owner;
    var $items;

    function Cart()
    {
        $this->todays_date = date("Y-m-d");
        $this->name = $GLOBALS['firstname'];
        /* etc. . . */
    }
}
?>

, . new.

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

$another_cart = new Cart;
$another_cart->add_item("0815", 3);

$cart $another_cart, Cart. add_item() $cart 1 10 $cart. 3 0815 $another_cart.

$cart, $another_cart add_item(), remove_item() . . . README.TXT, . , , , , : PHP, /global namespace, ->. , $cart->items $another_cart->items . , $cart->items, $cart->$items, PHP dollar.

// ,   $
$cart->items = array("10" => 1); 

// ,  $cart->$items  $cart->""
$cart->$items = array("10" => 1);

// ,      ,   :
// $cart->$myvar  $cart->items
$myvar = 'items';
$cart->$myvar = array("10" => 1);

, : Cart , $cart $another_cart. , $cart->items Cart. , , $this, ' ' ' '. '$this->items[$artnr] += $num' ' $num $artnr ' ' $num $artnr '.

: . Class/Object-.


extends
php

: 3119 .

: - |  -