0

when I run the below code I got error in line echo $attribute; The error code: "Catchable fatal error: Object of class SomeShape could not be converted to string in ": What wrong in this code? Thanks.

<?php

   class Shape
    {
      static public $width;
      static public $height;
    }

 class SomeShape extends Shape
    {
         public function __construct()
      {
        $test=self::$width * self::$height;
        echo $test;
        return $test;
      }
    }

    class SomeShape1 extends Shape
    {
         public function __construct()
      {
        return self::$height * self::$width * .5;
      }
    }

    Shape::$width=60;
    Shape::$height=5;
    echo Shape::$height;
    $attribute = new SomeShape;
    echo $attribute;
    $attribute1 = new SomeShape1;
    echo $attribute1;
?>
israel love php
  • 457
  • 3
  • 6
  • 17

4 Answers4

1

Don't do a return in a constructor.

If you want to echo a value, trys to add a __toString() function (manual)

adrien
  • 4,399
  • 26
  • 26
1

What you are trying to do is echo an object, its like you are echoing an array(worst than echoing an array since echoing an object throuws error), while what you should be doing is accessing it's attribute or method etc. However if u wanna c what is in your object, you gotto use var_dump instead of echo .

In short, echo $attribute is wrong. Use var_dump($attribute)

Falcon
  • 259
  • 3
  • 8
  • Ok I found solution ,I don't want to add more attribute to class shape but this solve my problem as I wish.may be there is more similar solution I will love to see. but this is my idea I define in class shape "public $attribute;" Inside the class SomeShape I wrote on "public function __construct()" "$this->attribute=self::$width * self::$height;" In the main scope I wrote "echo $object->attribute."
    ";"
    – israel love php May 07 '12 at 10:06
1

You cannot echo an object without implementing the __toString method.

Alternatively you could var_dump the object:

var_dump($attribute);

But what I think you are actually trying to do is more like this:

class Shape {
    public $width;
    public $height;

    public function __construct($width, $height) {
        $this->width = $width;
        $this->height = $height;
    }
}

class SomeShape extends Shape {
    public function getArea() {
        return $this->width * $this->height;
    }
}

class SomeShape1 extends Shape {
    public function getHalfArea() {
        return $this->width * $this->height * .5;
    }
}

$shape = new SomeShape(10, 20);
echo $shape->getArea();

$shape = new SomeShape1(10, 20);
echo $shape->getHalfArea();
Petah
  • 45,477
  • 28
  • 157
  • 213
0

the solution that I found is: I don't want to add more attribute to class shape but this solve my problem as I wish.may be there is more similar solution I will love to see. but this is my idea I define in class shape "public $attribute;" Inside the class SomeShape I wrote on "public function __construct()" "$this->attribute=self::$width * self::$height;" In the main scope I wrote "echo $object->attribute."
";"

<?php

   class Shape
    {
      static public $width;
      static public $height;
      public $attribute;
    }

 class SomeShape extends Shape
    {

         public function __construct()
      {
        $this->attribute=self::$width * self::$height;
      }
    }

    class SomeShape1 extends Shape
    {
         public function __construct()
      {
        $this->attribute=self::$width * self::$height * .5;
      }
    }

    Shape::$width=60;
    Shape::$height=5;


    $object = new SomeShape;
    echo $object->attribute."<br />";

    $object1 = new SomeShape1;
    echo $object1->attribute;
?>
israel love php
  • 457
  • 3
  • 6
  • 17