LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2005, 02:21 PM   #1
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
php classes, arrays


Hey everyone, I have no idea what I'm doing wrong
and I feel really stupid, have looked at php's manual
but don't know what to do.

In the code below, if you uncomment the line in the
function setRow(), it gets stuck in an infinite loop. As
it turns out, my assignment to what is supposed to be
one of the member variables (an array, $data), is actually
assigning to one of the other member variables ($numRows).

WTF is that?!!!

I'm a c(++) background, so this syntax is kinda weird to
me (having to put $this before everything, so annoying!).

The program's supposed to check if the (2-d) array is big enough,
and if not add enough rows (however many user wants). Can
someone please correct my php? Thanks.


PHP Code:
<?php
// classes for making and printing tables

class cell
{
    public 
$rspan$cspan$data;

    public function 
__construct()
    {
        
$argcfunc_num_args();
        
$argsfunc_get_args();

        
$d= ($argc 0)? $args[0] : null;
        
$r= ($argc 2)? $args[1] : null;
        
$c= ($argc 2)? $args[2] : null;

        
$data$d;
        
$rspan= ($r > -1)? $r 0;
        
$cspan= ($c > -1)? $c 0;
    }
}


class 
table
{
    private 
$numRows$numCols$data;
    public 
$htmlAtts$htmlRowClass;


    public function 
__construct()
    {
        
$argcfunc_num_args();
        
$argsfunc_get_args();

        
$this->$data= array();
        
$this->$htmlAtts= array();
        
$this->$htmlRowClass= array();

        if(
$argc 2) {$r$c0;}
        else
        {
            
$r$argv[0];
            
$c$argv[1];
        }

        
$this->$numRows= ($r > -1)? $r 0;
        
$this->$numCols= ($c > -1)? $c 0;
    }


    
// set a row's cells to the given array
    
public function setRow($r$arr)
    {
        print 
'r:        'print_r($rTRUE) ."\n";
        print 
'arr:      'print_r($arrTRUE) ."\n";
        print 
'num rows: 'print_r($this->$numRowsTRUE) ."\n\n\n";

        if(
$r 0) return FALSE;
        if(
$r >= $this->$numRows)  // add enough space
        
{
            for(
$icount($this->$data); $i <= $r$i++)
                
$this->$data[$i]= array();
            
$this->$numRows$r 1;
        }
        for(
$i0$i count($arr); $i++)
            
//$this->$data[$r][$i]= new cell($arr[$i]);    // PROBLEM HERE
        
return TRUE;
    }


    public function 
htmlStr()
    {
        print 
"num rows: "print_r($this->$numRowsTRUE) ."\n\nnum cols: "$this->$numCols ."\n\n";
        
$ret'<table';
        foreach(
$this->$htmlAtts as $k => $v) {$ret .= $k=\"$v\"";}
        
$ret .= '>';

        for(
$r0$r $this->$numRows$r++)
        {
            
//print "fjksjl";
            
$rowClass'';
            
$xcount($this->$htmlRowClass);
            if(
$x 0$rowClass' class="'$this->$htmlRowClass[$r $x] .'"';

            
$ret .= '<tr'$rowClass .'>';

            for(
$c0$c $this->$numCols$c++)
            {
            
//print "rioweuprw";
                
$temp$this->$data[$r][$c];

                if(
$temp !== null  &&  $temp->$rspan 0  &&  $temp->$cspan 0)
                    
$ret .= '<td>'htmlentities($temp->$data) .'</td>';
            }

            
$ret .= '</tr>';
        }

        return 
$ret '</table>';
    }
}
?>

<html><body>


<?php
$t
= new table();
$t->setRow(0, array(123));
$t->setRow(1, array(456));
$t->setRow(2, array(789));
print 
$t->htmlStr();


?>




</body></html>
 
Old 05-25-2005, 11:22 PM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
count(arr$) is a function that is being re-evaluated each time through the loop. You are adding a cell each time through the loop, so count(arr$) increases each time through. Hence you never reach the end.
Replace the function with a constant and then I'll bet it works.
 
Old 05-26-2005, 08:54 AM   #3
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
jiml8:

Thanks, but $arr isn't being changed there, what's being
changed is $this->$data[$r][0] ..... $this->$data[$r][n-1], where
n is the length of $arr. But $arr is still constant in that function.

For example, this does the same thing:
PHP Code:
for($i0$jcount($arr); $i $j$i++)
    
$this->$data[$r][$i]= new cell($arr[$i]);    // PROBLEM HERE 

That was the spot you're talking about right?


Anyway, does this have something to do with the variable being
allocated and then overwritten unintentionally (has anyone seen
that before in php) or am I just messing it up somewhere?

thanx everyone.
 
Old 05-26-2005, 01:58 PM   #4
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
Wrote a little more but haven't figured it out yet.

It no longer gets in an infinite loop, but for some reason
$this->$data never gets made into an array, so
$this->$data[n] can never get made into arrays themselves.

Probably some stupid syntax mistake I have, anyone see
what it is? Thanx



PHP Code:
<?php
// classes for making and printing tables

class cell
{
    public 
$rspan$cspan$data;

    public function 
__construct()
    {
        
$argcfunc_num_args();
        
$argsfunc_get_args();

        
$d= ($argc 0)? $args[0] : null;
        
$r= ($argc 2)? $args[1] : null;
        
$c= ($argc 2)? $args[2] : null;

        
$data$d;
        
$rspan= ($r > -1)? $r 0;
        
$cspan= ($c > -1)? $c 0;
    }
}


class 
table
{
    private 
$numRows$numCols$data;
    public 
$htmlAtts$htmlRowClass;


    public function 
__construct()
    {
        
$argcfunc_num_args();
        
$argsfunc_get_args();

        
$this->$data= array();
        
$this->$htmlAtts= array();
        
$this->$htmlRowClass= array();

        if(
$argc 2) {$r$c0;}
        else
        {
            
$r$argv[0];
            
$c$argv[1];
        }

        
$this->$numRows= ($r > -1)? $r 0;
        
$this->$numCols= ($c > -1)? $c 0;

        print 
"finished __construct()\n\n\n";
        
print_r($this);
    }


    
// set a row's cells to the given array
    
public function setRow($r$arr)
    {
        print 
'r:        'print_r($rTRUE) ."\n";
        print 
'arr:      'print_r($arrTRUE) ."\n";
        print 
'num rows: 'print_r($this->$numRowsTRUE) ."\n\n\n";

        print 
"data: "print_r($this->$dataTRUE) . "\n\n\n\n\n";

        if(
$r 0) return FALSE;
        if(
$r >= $this->$numRows)  // add enough space
        
{
            for(
$icount($this->$data); $i <= $r$i++)
                
$this->$data[$i]= array();
            
$this->$numRows$r 1;
        }

        for(
$i0$jcount($arr); $i $j$i++)
            
array_push($this->$data[$r], new cell($arr[$i]));    // PROBLEM HERE
        
return TRUE;
    }


    public function 
htmlStr()
    {
        print 
"num rows: "print_r($this->$numRowsTRUE) ."\n\nnum cols: "$this->$numCols ."\n\n";
        
$ret'<table';
        foreach(
$this->$htmlAtts as $k => $v) {$ret .= $k=\"$v\"";}
        
$ret .= '>';

        for(
$r0$r $this->$numRows$r++)
        {
            
//print "fjksjl";
            
$rowClass'';
            
$xcount($this->$htmlRowClass);
            if(
$x 0$rowClass' class="'$this->$htmlRowClass[$r $x] .'"';

            
$ret .= '<tr'$rowClass .'>';

            for(
$c0$c $this->$numCols$c++)
            {
            
//print "rioweuprw";
                
$temp$this->$data[$r][$c];

                if(
$temp !== null  &&  $temp->$rspan 0  &&  $temp->$cspan 0)
                    
$ret .= '<td>'htmlentities($temp->$data) .'</td>';
            }

            
$ret .= '</tr>';
        }

        return 
$ret '</table>';
    }
}
?>

<html><body>


<?php
$t
= new table();
$t->setRow(0, array(123));
$t->setRow(1, array(456));
$t->setRow(2, array(789));
print 
$t->htmlStr();
?>

</body></html>
 
Old 05-27-2005, 11:44 AM   #5
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
Okay, I made an example script just to see if I'm doing my
php syntax right, but I'm not, and php.net and tutorials
aren't really helping.

If anyone can explain what I'm doing wrong here, I'd really
appreciate it. And if anyone can help me with the other, bigger
program (one I'm really working on) that would be great too.

Also anyone know why print_r doesn't print the class members
when given a class? My own tostr() func says the members
are set to a number (wrong number though), but print_r
lists that number in private, what's that about?

Here's my version:
Code:
PHP 5.0.4 (cli) (built: Mar 31 2005 02:45:48)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies
Here's the code I can't get working:
PHP Code:
<?php

// example class
class point
{
    public 
$x$y;


    public function 
__construct($X0$Y0)
    {
        
$this->$x$X;  $this->$y$Y;
    }


    public function 
add($a)  // add a point or scalar
    
{
        
$ret= new point();

        if(
$a instanceof point)
        {
            
$ret->$x$this->$x $a->$x;
            
$ret->$y$this->$y $a->$y;
        }

        else if(
is_double($a)  ||  is_int($a))
        {
            
$ret->$x$this->$x $a;
            
$ret->$y$this->$y $a;
        }

        return 
$ret;
    }


    public function 
mul($a)  // multiply a point or scalar
    
{
        
$ret= new point();

        if(
$a instanceof point)
        {
            
$ret->$x$this->$x $a->$x;
            
$ret->$y$this->$y $a->$y;
        }

        else if(
is_double($a)  ||  is_int($a))
        {
            
$ret->$x$this->$x $a;
            
$ret->$y$this->$y $a;
        }

        return 
$ret;
    }


    public function 
tostr()  // to readable string
    
{
        return 
'('$this->$x .', '$this->$y .')';
    }
}



$z1= new point;
$z2= new point();
$a= new point(34);
$b= new point(78);



$res= array(
    
'z1'      => $z1,
    
'z2'      => $z2,
    
'a + b'   => $a->add($b),
    
'b + a'   => $b->add($a),
    
'a * b'   => $a->mul($b),
    
'b * a'   => $b->mul($a),
    
'a + 4.8' => $a->add(4.8),
    
'b - 4.8' => $b->add(-4.8));

$res['mid']= $res['a + b']->mul(0.5);



// cross our fingers and print the results
foreach($res as $k => $v)
    print 
"$k: "$v->tostr() ."\n"print_r($vTRUE) ."\n\n";


?>

Last edited by towlie; 05-27-2005 at 11:49 AM.
 
Old 05-28-2005, 10:43 AM   #6
LLS
Member
 
Registered: Sep 2003
Location: North America
Distribution: FC6 FC7 F8
Posts: 75

Rep: Reputation: 15
Have you looked through this?

http://www.phpfreaks.com/phpmanual/page/ref.classobj.html

I am interested to see what your solution is.
 
Old 05-28-2005, 11:21 AM   #7
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
LLS, thanks for the link, I will check it out. If I figure
this out I'll post the solution when finished.
 
Old 05-29-2005, 03:42 PM   #8
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
yeah, like i thought, dumb mistake

you have to use this:
Code:
$myClass->myVar;
$myClass->myFunc();
not this:
Code:
$myClass->$myVar;
$myClass->myFunc();
 
Old 05-29-2005, 04:34 PM   #9
LLS
Member
 
Registered: Sep 2003
Location: North America
Distribution: FC6 FC7 F8
Posts: 75

Rep: Reputation: 15
If you have the chance, could you point at a section of code in your second example, or first that this revelation applies to? Thanks much for sharing your solution.
 
Old 05-29-2005, 06:07 PM   #10
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
So from the 2nd example I had:
Code:
// this is WRONG
if($a instanceof point)
{
    $ret->$x= $this->$x + $a->$x;
    $ret->$y= $this->$y + $a->$y;
}


// the above should be changed to this
if($a instanceof point)
{
    $ret->x= $this->x + $a->x;
    $ret->y= $this->y + $a->y;
}
 
Old 06-07-2005, 11:10 AM   #11
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
Here is the result of my work if anyone is interested:
http://galaxy.cs.lamar.edu/~ja137401...057/index.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and classes Boby Programming 5 03-23-2005 05:15 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
Arrays of Functions and Classes problem. (C++) -=zAe=- Programming 2 02-24-2005 10:21 AM
php arrays and mysql simpleguy Programming 2 07-08-2004 06:28 AM
Static Arrays in php Not working ElementNine Programming 4 03-05-2004 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration