pahunrepublic Posted June 9, 2011 Share Posted June 9, 2011 I'm just self-learning OOP PHP and I have this sample code and I get this error Fatal error: Cannot access empty property in Rectangle.php on line 25: Rectangle.php <?php # Script 6.3 - Rectangle.php /* This page defines the Rectangle class. The class contains two attributes: width and height. The class contains four methods: - set_size() - get_area() - get_perimeter() - is_square() */ class Rectangle { //Declare attributes. public $width = 0; public $height = 0; //Method to set the dimensions. function set_size($w= 0, $h = 0) { $this->$width = $w; $this->$height = $h; } //Method to calculate and return the area function get_area(){ return ($this->width * $this-height); } //Method to calculate and return the perimeter. function get_perimeter() { return (($this->width + $this->height) * 2); } //final method that indicates if the rectangle is also a square function is_square() { if ($this->width == $this->height) { return true; } else { return false; } } }//end of Rectangle class ?> and rectangle1.php <?php require_once('Rectangle.php'); $width = 42; $height = 7; // Print a little introduction: echo "<h3>With a width of $width and a height of $height...</h3>"; // Create a new object: $r = new Rectangle(); // Assign the rectangle dimensions. $r->set_size($width, $height); // Print the area. echo '<p>The area of the rectangle is ' . $r->get_area() . '</p>'; // Print the perimeter. echo '<p>The perimeter of the rectangle is ' . $r->get_perimeter() . '</p>'; // Is this a square? echo '<p>This rectangle is'; if ($r->is_square()) { echo 'also'; } else { echo 'not'; } echo 'a square.</p>'; // Delete the object: unset($r); ?> Any idea why do I get this error. I can't trace the bug because as I said I'm beginner in OOP PHP. Please help Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/ Share on other sites More sharing options...
xyph Posted June 9, 2011 Share Posted June 9, 2011 Here's a problem function set_size($w= 0, $h = 0) { $this->$width = $w; $this->$height = $h; } You want function set_size($w= 0, $h = 0) { $this->width = $w; $this->height = $h; } Also function get_area(){ return ($this->width * $this-height); } Should be function get_area(){ return ($this->width * $this->height); } Pay attention to both the file the error is in, and the line. It should help you isolate your issue Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/#findComment-1227562 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 if the syntax errors that xyph pointed out do not solve you issue, please post the exactl line that the error is occurring on Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/#findComment-1227563 Share on other sites More sharing options...
wildteen88 Posted June 9, 2011 Share Posted June 9, 2011 You're not setting the width and height properties within the set_size method correctly function set_size($w= 0, $h = 0) { $this->$width = $w; $this->$height = $h; } You should be using $this->width and $this->height not $this->$width and $this->$height Another issue you have is on this line return ($this->width * $this-height); You have left of the > after $this- Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/#findComment-1227564 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 You're not setting the width and height properties within the set_size method correctly function set_size($w= 0, $h = 0) { $this->$width = $w; $this->$height = $h; } You should be using $this->width and $this->height not $this->$width and $this->$height Another issue you have is on this line return ($this->width * $this-height); You have left of the > after $this- xyph pointed these exact issues out wildteen... Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/#findComment-1227573 Share on other sites More sharing options...
wildteen88 Posted June 9, 2011 Share Posted June 9, 2011 xyph pointed these exact issues out wildteen... Oh.. I didn't realise xyph had replied. Must of posted as I was typing my reply. Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/#findComment-1227575 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 xyph pointed these exact issues out wildteen... Oh.. I didn't realise xyph had replied. Must of posted as I was typing my reply. no worries, happens to me all of the time..heh Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/#findComment-1227577 Share on other sites More sharing options...
pahunrepublic Posted June 10, 2011 Author Share Posted June 10, 2011 Thank You all xiyph, fugix. It seems the problems were all typo related. Wow I didn't copy well the script from the book. Quote Link to comment https://forums.phpfreaks.com/topic/238901-why-do-i-get-this-error-fatal-error-cannot-access-empty-property-in/#findComment-1227770 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.