# Move method in php ??



## Hale88 (Dec 14, 2009)

*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


----------



## Hale88 (Dec 14, 2009)

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();


----------



## Disparia (Dec 14, 2009)

Sounds like they just want you to create a method that modifies the x and y.


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

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


----------



## Hale88 (Dec 14, 2009)

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


----------



## Disparia (Dec 14, 2009)

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 (Dec 14, 2009)

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?


----------



## Disparia (Dec 15, 2009)

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 (Dec 15, 2009)

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


----------



## Disparia (Dec 15, 2009)

Oh... I see now.

Will check them out.


----------

