Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on classes

Status
Not open for further replies.

jdhilljr

Programmer
Sep 1, 2003
296
US
I am playing with classes but have an odd problem and wondering if anyone can tell me why this is happening. I have 4 classes they all work and give the right answer but only D and M echo the text the others only echo the answer.

Code:
<?php
class A
{
    function data_insert($a,$b)
    {
        if (isset($this)) {
            echo get_class($this).' '.$a.' plus '.$b.' = '.$a+$b;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}
class S
{
   function data_insert($a,$b)
    {
        if (isset($this)) {
            echo get_class($this).' '.$a.' minus '.$b.' = '.$a-$b;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}
class M
{
   function data_insert($a,$b)
    {
        if (isset($this)) {
            echo get_class($this).' '.$a.' times '.$b.' = '.$a*$b;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}
class D
{
   function data_insert($a,$b)
    {
        if (isset($this)) {
            echo get_class($this).' '.$a.' divided by '.$b.' = '.$a/$b;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}
$az=0;
while($az<100) {
$ax=rand(1,4);
$class=array(1=>'A',2=>'S',3=>'M',4=>'D');
$d=rand(10,100);
$e=rand(10,100);
$c = new $class[$ax]();
$c->data_insert($d,$e);
echo '<br>';
$az++;
}

?>

If you can't stand behind your troops, stand in front of them.
Semper Fidelis

Jim
 
Hi

That has absolutely nothing to do with classes or with OOP in general.

It is simply matter of Operator Precedence : string concatenation has lower priority than multiplication and division but higher priority than addition and subtraction. So the order of evaluation influence the result :
Code:
Interactive shell

[blue]php >[/blue] $a=3; $b=2; var_dump(''.$a+$b,''.$a-$b,''.$a*$b,''.$a/$b);
int(5)
int(1)
string(1) "6"
string(3) "1.5"
Lesson to learn : be kind with the PHP interpreter and do not put it to concatenate things when is not necessary. Simply enumerate the [tt]echo[/tt] parameters separated with comma ( , ) instead of concatenating them :
Code:
[b]echo[/b] [COLOR=darkgoldenrod]get_class[/color][teal]([/teal][navy]$this[/navy][teal]),[/teal][green][i]' '[/i][/green][teal],[/teal][navy]$a[/navy][teal],[/teal][green][i]' plus '[/i][/green][teal],[/teal][navy]$b[/navy][teal],[/teal][green][i]' = '[/i][/green][teal],[/teal][navy]$a[/navy][teal]+[/teal][navy]$b[/navy][teal];[/teal]


Feherke.
 
Yeah I had just figured that out with the + and - sign

Code:
<?php
class Addition
{
    function data_insert($a,$b)
    {
        if (isset($this)) {
		$answer=$a+$b;
            echo get_class($this).' '.$a.' added to '.$b.' = '.$answer;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}
class Multiplication
{
   function data_insert($a,$b)
    {
        if (isset($this)) {
            $answer=$a*$b;
            echo get_class($this).' '.$a.' multiplied by '.$b.' = '.$answer;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

class Division
{
   function data_insert($a,$b)
    {
        if (isset($this)) {
            $answer=$a/$b;
            echo get_class($this).' '.$a.' divided by '.$b.' = '.$answer;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}
class Subtraction
{
   function data_insert($a,$b)
    {
        if (isset($this)) {
           $answer=$a-$b;
            echo get_class($this).' '.$a.' minus '.$b.' = '.$answer;
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

$az=0;
while($az<100) {
$ax=rand(1,4);
$class=array(1=>'Addition',2=>'Subtraction',3=>'Multiplication',4=>'Division');
$d=rand(10,100);
$e=rand(10,100);
$c = new $class[$ax]();
$c->data_insert($d,$e);
echo '<br>';
$az++;
}

?>

If you can't stand behind your troops, stand in front of them.
Semper Fidelis

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top