Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 5th, 2004, 10:30 AM
Registered User
 
Join Date: Mar 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Vector class php

Hi, any one knows where can I download the vector.class.php?

 
Old March 6th, 2004, 09:50 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You need to be more specific. Is this a file mentioned in the Wrox Beginning PHP 4 book? Unfortunately, most of use answering questions don't even have the book. If it is -- use the `Download Code` link at the top of this page.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old March 7th, 2004, 02:23 AM
Registered User
 
Join Date: Mar 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well, I was trying out the codes on php fundermentals,(book titled: beginning php4 databases), the example used vectors class to create textboxes and submit buttons, however, this vector class is not found in the library of my installed php source, either is the search results on the web.
thus I am not sure whether there is such a class available or is there another way to create all the GUIs?

thanks! =)

 
Old March 12th, 2004, 03:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, what do you mean by vector, then? Traditionally, "vector" is pretty much synonymous with "array".

If you need accessor functions of an object, such as push, pop, top, is_empty, etc... (which more define a stack, queue, or deque container), then you can write these using a standard PHP array.

class Stack
{
    var $st; // the stack

    function Stack()
    {
       $st = array();
    }

    function size()
    {
        return count($this->st);
    }

    function is_empty()
    {
        return $this->size() == 0;
    }

    // add a new item to the stack
    function push($item)
    {
        $st[] = $item;
    }

    // get the item on the top of the stack (without removing it)
    function top()
    {
        $ret = FALSE;
        if (! $this->is_empty())
        {
            $ret = $this->st[$this->size() - 1];
        }

        return $ret;
    }

    // remove item from the stack
    function pop()
    {
        $ret = $this->top();
        if (! $this->is_empty())
        {
           unset($this->st[$this->size() - 1]);
        }
    }
}


Note that I didn't test this code -- I just typed it into the reply window. But it should give you an idea of how to write your own data types using existing primitive types.

Also, you should notice the inherent problem with normal function semantics, that is, that functions can only return one value.

Our functions return FALSE when there's a problem (e.g. popping an empty stack). PHP doesn't let you throw exceptions, so we return FALSE to signify a problem. "So what?" you ask? Well, what if the stack actually contained the value FALSE? In that case, you can't distinguish from a valid or invalid operation.

This is why many C functions use pointers to set an out parameter, and use the return value for error codes.

In PHP, we can use references to achieve the same thing:

class Stack
{
    var $st; // the stack

    function Stack()
    {
       $st = array();
    }

    function size()
    {
        return count($this->st);
    }

    function is_empty()
    {
        return $this->size() == 0;
    }

    // add a new item to the stack
    function push($item)
    {
        $st[] = $item;
    }

    // get the item on the top of the stack (without removing it)
    function top(& $item)
    {
        $ret = FALSE;
        if (! $this->is_empty())
        {
            $item = $this->st[$this->size() - 1];
            $ret = TRUE;
        }

        return $ret;
    }

    // remove item from the stack
    function pop(& $item)
    {
        $ret = FALSE;
        if (! $this->is_empty())
        {
           $item = $this->top();
           unset($this->st[$this->size() - 1]);
           $ret = TRUE;
        }
    }
}


See the difference? We pass in $item as a reference to our functions, and assign our real return value to it. We use the function return value as an error check -- we return FALSE if there's a problem, and TRUE if everything's okay.



Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to customise class.database.php raoof PHP Databases 0 December 31st, 2007 11:48 AM
class.SimpleMail.php problem mrthis PHP Databases 2 December 28th, 2005 09:30 AM
Vector problem ronnie5 C++ Programming 3 November 9th, 2005 08:18 AM
Trouble Using Vector Iain C++ Programming 0 April 7th, 2004 08:12 PM
vector.add(); chikodi Servlets 2 October 23rd, 2003 02:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.