Running Class & Object in Same File


#1

Hello Everyone,
I’m new to PHP programming and I’d like your assistance. I’m trying learning about classes. I wrote the following script and create a new object, all in the same file, however, after running it i get the following error message: Parse error : syntax error, unexpected ‘}’ in C:\Users\bgeor\Desktop\WebServer\classTest1.php on line 9.
Double checking on my script it seems everything is fine (obviously it’s not, since i keep on getting the same error). Thanks

<?php
class Rectangle {
	//Declare properties
	private $length=0;
	private $width=0;
	//Method declaration
	 function getPerimeter() {
		return (2*($this->length + $this->width))
	}
	//Method to get the area
	function getArea(){
		return ($this->length*$this->width)
	}
	//Create new object from Rectangle class
	$rect = new Rectangle;
	//Get the object property value
	echo $rect->length;
	echo $rect->width;
	//Set object property values
	$rect->length=30;
	$rect->width=20;
	//Call the object methods
	echo $rect->getPerimeter();
	echo $rect->getArea();
									   
}

?>

#2

Please make sure the code you paste is properly formatted. You can do this by selecting the code and clicking the code button in the editor, or by wrapping it in

```php
code here
```
<?php
class Rectangle
{

	// Declare properties
	private $length = 0;
	private $width = 0;

	// Method declaration
	function getPerimeter()
	{
		return (2 * ($this->length + $this->width))
	}

	// Method to get the area
	function getArea()
	{
		return ($this->length * $this->width)
	}

	// Create new object from Rectangle class
	$rect = new Rectangle;

	// Get the object property value
	echo $rect->length;
	echo $rect->width;

	// Set object property values
	$rect->length = 30;
	$rect->width = 20;

	// Call the object methods
	echo $rect->getPerimeter();
	echo $rect->getArea();
}

I think you would benefit from using a proper IDE (phpstorm, netbeans, etc). It will actually highlight syntax errors so you can see them fore even trying to run the code.


Your errors:

function getPerimeter()
{
	return (2 * ($this->length + $this->width)) <-- missing semicolon
}

function getArea()
{
	return ($this->length * $this->width) <-- missing semicolon
}

Your next error:
You can’t instantiate an object and code inside the object like you’re doing. This code should be outside the class (preferably outside of the class file)

        // Create new object from Rectangle class
	$rect = new Rectangle;

	// Get the object property value
	echo $rect->length;
	echo $rect->width;

	// Set object property values
	$rect->length = 30;
	$rect->width = 20;

	// Call the object methods
	echo $rect->getPerimeter();
	echo $rect->getArea();

#3

Hello JimL,
Thank you for your help, I implemented the changes you suggested and indeed had the code run correctly. classTest1

As for the IDE I’m using Codelobster and run it on the lightweight webserver QuickPHP. I intend to move to PhpStorm or WampServer once I have gained better command of php.

Regards,