tags:

views:

435

answers:

4

Hello Folks.

I'm new to PHP and web scripting in general so this a newb question.

Currently i'm a creating an instance to an object, yet when I call the constructor the script slienty shuts down... it doesn't call the next function and I don't know why. Any help would be welcome.

Here is the code.

<?php
class product {
    var $ssProductName;
    var $ssVendorName;
    var $ssDescr;
    var $ssURI;

    // Clean constructor, strings must be cleaned before use
    function __construct($ssProd, $ssVendor, $ssD, $ssU) {
        $this->$ssProductName = $ssProd;
        $this->$ssVendorName = $ssVendor;
        $this->$ssDescr = $ssD;
        $this->$ssURI = $ssU;
    }

    // print a table of the values
    function DisplayOneEntry() {

        echo '<table border="1">
                    <tr>
                    <td>'.$this->$ssProductName.'</td>
                    <td>'.$this->$ssVendorName.'</td>
                    <td>'.$this->$ssDescr.'</td>
                    <td>'.$this->$ssURI.'</td>
                    </tr>
                    </table>';
    }

}

echo "<HTML>";
echo "A";
$newP = new product("Redhat", "Redhat corp", "Leader in", "www.redhat.com");
echo "B";
$newP->DisplayOneEntry();

echo "</HTML>";
?>

But the output is just:

<HTML>
A

Then nothing else.

This is running on a hosting provider using php 5.2.9 and Apache 2.2.

+10  A: 

You need to access the member variables with:

$this->variableName

Not:

$this->$variableName
jbradaric
Thankyou all for your help. And yes now I have display_error turned on. :-)
+3  A: 
    $this->$ssProductName = $ssProd;

should be

    $this->ssProductName = $ssProd;

no $ after the ->

Galen
+1  A: 

The syntax $this->$foo is a variable variable referencing the class attribute with the name of the value of $foo. So if $foo has the value bar, $this->$foo would reference $foo->bar and not $this->foo.

So just remove the $ after $this-> and it should work.

Gumbo
A: 

That is because your php script is erroneous. To catch such errors, you should run it on a debugging server (with display_errors set to On) or use other logging methods.

However, the main problem is you are accessing object members the wrong way; there's no second dollar sign. Instead of

$this->$ssProductName = $ssProd;

use

$this->ssProductName = $ssProd;
phihag