Set Method is being ignored somehow

Hi, guys… This is my first post, just having a little trouble with some code, hope someone may know what I’m doing wrong…

FULL DISCLOSURE: This was an assignment for a class I’m taking online, but it has already been submitted and graded. I was given full credit for it with no notes from the instructor, so whatever I’m doing wrong must not be too obvious. I just want to make sure that I get this corrected moving forward in case the same issue is more integral to the script later on. (I’m here instead of talking to my teacher because, as it’s an online class, it can sometimes take a few days to hear back from her).

These were the instructions:

[code]Create a class called Rectangle. This class will have two attributes length and width. Create set and get methods for the attributes. The set methods make sure the attributes only contain values between 1 and 100. The get methods return the value of the attribute.

Create methods that calculate and return a value for perimeter and area.

The attributes have private access and all the methods have public access.

Create a constructor that allows you to pass values for the length and the width. Give the length and the width default values of 1.

Create two instances of the Rectangle class. Call all the methods and use echo or print to display values to make sure your code works correctly.[/code]

What I came up with does all of that, except for the set methods checking the validity of the values. It will accept any numbers. I did some testing, and it seems that the value is being assigned by the Constructor and then PHP is totally ignoring the set methods.
Here is the code I wrote:


class Rectangle { //Rectangle Class Defined here.
private $length; //Private attribite for length.
private $width; //Private Attribute for width.

public function __construct($lvalue = 1, $wvalue = 1) {							//Constructor with a default value of 1 for length & width.
	$this->length = $lvalue;
	$this->width = $wvalue;

public function setLength($lvalue) {											//Sets length (between 1 & 100)
	if (($lvalue >= 1) && ($lvalue <= 100)) {
		$this->length = $lvalue;

public function getLength() {													//Returns value for Length.
	return $this->length;

public function setWidth($wvalue) {												//Sets width (between 1 & 100)
	if (($wvalue >= 1) && ($wvalue <= 100)) {
		$this->width = $wvalue;

public function getWidth() {													//Returns value for Width.
	return $this->width;

public function getPerimeter() {
	return $this->length + $this->length + $this->width + $this->width; 		//Calculates Perimeter (2L + 2W)

public function getArea() {
	return $this->length * $this->width;										//Calculates Area (L*W)


$defaultRectangle = new Rectangle(); //New instance of Rectangle with default values.

$length = $defaultRectangle->getLength();
$width = $defaultRectangle->getWidth();
$perimeter = $defaultRectangle->getPerimeter();
$area = $defaultRectangle->getArea();
echo "The length of the rectangle with default values is $length
echo "The width of the rectangle with default values is $width
echo "The perimeter of the rectangle with default values is $perimeter feet.
echo "The area of the rectangle with the default values is $area sq. ft.

print “


$customRectangle = new Rectangle(5,9); //New instance of Rectangle with custom L & W values.

$length = $customRectangle->getLength();
$width = $customRectangle->getWidth();
$perimeter = $customRectangle->getPerimeter();
$area = $customRectangle->getArea();
echo "The length of the custom rectangle is $length
echo "The width of the custom rectangle is $width
echo "The perimeter of the custom rectangle is $perimeter feet.
echo “The area of the custom rectangle is $area sq. ft.”;


If anyone here can pinpoint what I’m doing wrong, I would greatly appreciate the input.



Hi there,

Just to check, are you aware that in the example you gave you aren’t actually calling the “setLength” or “setWidth” functions? If you aren’t doing that then of course the length and width will stay the same as when you originally defined them O.o

Sponsor our Newsletter | Privacy Policy | Terms of Service