Jump to content

Recommended Posts

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

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

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-

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... :smoker:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.