Welcome, guest | Sign In | My Account | Store | Cart

As `throw new Exception' is just avaliable in php 5, for use php 4 use that call shoud be replaced.

PHP, 56 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
# Author: Andre Souza <ienliven@gmail.com>
# SingleLinkedList Proposal

class SingleLinkedList
{
    var $_vars;
    var $_next;
    
    function SingleLinkedItem()
    {
        $this->_vars = array();
        $this->_next = null;
    }
    
    private function __get($name)
    {
        if($name == "_last")
        {
            $last =& $this;
            while($last->_next != null)
                $last =& $last->_next;
            return $last;
        }
        
        if(isset($this->_vars[$name])) return $this->_vars[$name];
        else 
        {
            throw new Exception("variable: '$name' not found.");
        }
    }
    
    private function __set($name, $value)
    {
        $this->_vars[$name] = $value;
    }
}


# USE:
for($i = 0; $i < 10; $i++)
{
    if(!isset($SLL))
    {
        $SLL = new SingleLinkedList();
        $SLL->name = "Son_{$i}";
    }
    else
    {
        $SLL->_last->_next = new SingleLinkedList($SLL);
        $SLL->_last->name = "Son_{$i}";
    }
}

var_dump($SLL);
?>

As `throw new Exception' is just avaliable in php 5, for use php 4 use that call shoud be replaced.

Created by Andre Wanderley de Souza on Fri, 15 Sep 2006 (MIT)
PHP recipes (51)
Andre Wanderley de Souza's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks