0

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.

4
  • I take it you forgot the leading <?php ? Btw, the trailing ?> is not necessary. Commented Jun 16, 2009 at 21:38
  • 1
    It looks like you have display_errors set to Off, or error_reporting showing nothing, or both of these problems. Commented Jun 16, 2009 at 21:40
  • 1
    I think the var keyword is deprecated in 5. Turn your errors on and provide those please :) through error_reporting(E_ALL) Commented Jun 16, 2009 at 21:40
  • There also seems to be missing ' on the end of your <td> You need to escape the line return Commented Jun 16, 2009 at 21:45

4 Answers 4

9

You need to access the member variables with:

$this->variableName

Not:

$this->$variableName
Sign up to request clarification or add additional context in comments.

1 Comment

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

should be

    $this->ssProductName = $ssProd;

no $ after the ->

Comments

1

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.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.