• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Move method in php ??

Hale88

New Member
Joined
Sep 4, 2009
Messages
153 (0.03/day)
Location
Somewhere in FL
Inheritance and Polymorphism help

what is move method in CLASS?

I tried to search in google but no luck:

"Create a move method in the Shape class that accepts two values. These values are added to x and y attributes to simulate moving the shape."

this is the code I got so far (please fix me if something is wrong with this code):

class shape {
private $name;
private $x;
private $y;

function __construct($x , $y) {
$this->X = $x;
$this->y = $y;
}
public function setName($name) {
$this-> name = $name ;
}
public function getName() {
return $this-> name ;
}
public function setX($x) {
$this-> x = $x ;
}

public function getX(){
return $this-> x ;
}
public function setY($y){
$this-> Y = $y;
}
public function getY(){
return $this-> Y ;
}

thanks
hale
 
Last edited:

Hale88

New Member
Joined
Sep 4, 2009
Messages
153 (0.03/day)
Location
Somewhere in FL
here are some update. I have to create a Shape.php, Circle.php, and Square.php and yes I did, here is the code: However, I cannot get them run for some reason ( I still need MOVE METHOD)

Testing code:

require_once('Shape.php');
require_once('Circle.php');
require_once('Square.php');

$myCircle = new Circle("aCircle", 2, 3, 5);
$mySquare = new Square("aSquare", 0, 0, 4);

echo $mySquare->getName();
echo "<br />";
echo "Area of square is ";
echo $mySquare->area();
echo "<br />";

$mySquare-> move(23, 45);
echo "Position of square is x = ";
echo $mySquare->getX();
echo " y = ";
echo $mySquare->getY();
$mySquare->setSide(6);
echo "<br />";
echo "Area of square is ";
echo $mySquare->area();
echo "<br />";
echo $myCircle->getName();
echo "<br />";
echo "Area of circle is ";
echo $myCircle->area();
echo "<br />";
$myCircle->move(15, 20);
echo "Position of circle is x = ";
echo $myCircle->getX();
echo " y = ";
echo $myCircle->getY();
$myCircle->setRadius(-2);
echo "<br />";
echo "Area of circle is ";
echo $myCircle->area();

Shape:
class Shape {
private $name;
private $X;
private $Y;

function move ($X,$Y) {
return $this->X;
return $this->Y;
}

function __construct($x,$y) {
$this->X = $x;
$this->Y = $y;
}
public function setName($name) {
$this-> name = $name ;
}
public function getName() {
return $this-> name ;
}

public function setX($X) {
$this-> name = $X ;
}
public function getX() {
return $this-> X ;
}

public function setY($Y) {
$this-> name = $Y;
}
public function getY() {
return $this-> Y ;
}

}

Circle:
require_once ('Shape.php');
class Circle extends Shape {

private $radius;


function __construct($name,$x,$y,$radius) {
Shape::__construct($name,$x,$y);
$this->radius = $radius;


}
public function setradius($radius) {
if(is_numeric($radius) && $radius > 0 ) {
$this->radius = $radius;
}
if(is_numeric($radius) && $radius < 0 ) {
$this->radius = 0;
}
}
public function getradius($radius) {
return $this-> radius ;
}

function getarea() {
return 3.14159 * $this-> radius * $this-> radius;
}


}
$myCircle = new Circle("aCircle", 2, 3, 5);
echo $myCircle->getradius();
echo "<br />";
echo $myCircle->getName();
?>

Square:

require_once ('Shape.php');
class Square extends Shape {

private $side;


function __construct($name,$x,$y,$side) {
Shape::__construct($name,$x,$y);
$this->side = $side;


}
public function setSide($side) {
if(is_numeric($side) && $side > 0 ) {
$this->side = $side;
}
if(is_numeric($side) && $side < 0 ) {
$this->side = 0;
}
}
public function getSide($side) {
return $this-> side ;
}

function getarea() {
return $this-> side * $this-> side;
}

}
$mySquare = new Square("aSquare", 0, 0, 4);
echo $mySquare->getSide();
echo "<br />";
echo $mySquare->getName();
 
Last edited:
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
Sounds like they just want you to create a method that modifies the x and y.

PHP:
public function move(x, y) {
	$this->X += x;
	$this->Y += y;
}

Your move method is just returning values, so nothing is changed.
 

Hale88

New Member
Joined
Sep 4, 2009
Messages
153 (0.03/day)
Location
Somewhere in FL
Actually, When I change the X and Y the Area will also change and so the position. However, I cannot get them to run for some reason.

thanks for help
hale
 
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
True, it doesn't get that far because your Square class doesn't have an area method ("echo $mySquare->area();"), it has a getarea method.
 

Hale88

New Member
Joined
Sep 4, 2009
Messages
153 (0.03/day)
Location
Somewhere in FL
OK I change it back to $mySquare->area() but still not working. This is the problem

"Fatal error: Non-abstract method Shape::move() must contain body in C:\xampp\htdocs\Final\Shape.php on line ##"

thanks
P/S: How can you put all three files together so you can you the TEST code to test them?
 
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
Create/edit a file with all the classes, removing any require or include functions. Will probably make things easier to troubleshoot.

Have you changed the code much from what you posted earlier?
 

Hale88

New Member
Joined
Sep 4, 2009
Messages
153 (0.03/day)
Location
Somewhere in FL
they ask me to create 3 separate file call: Shape.php, Circle.php, Square.php. They given me the Test File call index.php and tell me to using the test code to calculate the Circle and Square. I will send them via PM to you. Please help me check if I made any mistake. But I dont think I do.

thanks
 
Joined
Aug 10, 2007
Messages
4,267 (0.70/day)
Location
Sanford, FL, USA
Processor Intel i5-6600
Motherboard ASRock H170M-ITX
Cooling Cooler Master Geminii S524
Memory G.Skill DDR4-2133 16GB (8GB x 2)
Video Card(s) Gigabyte R9-380X 4GB
Storage Samsung 950 EVO 250GB (mSATA)
Display(s) LG 29UM69G-B 2560x1080 IPS
Case Lian Li PC-Q25
Audio Device(s) Realtek ALC892
Power Supply Seasonic SS-460FL2
Mouse Logitech G700s
Keyboard Logitech G110
Software Windows 10 Pro
Oh... I see now.

Will check them out.
 
Top