Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

use var_dump() to print out the objects before and after you call the methods.

Followup to this comment

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28314431.html#a39708244

Instead of using echo (which will call the __toString() method and will deliberately remove most of the character of the object), use var_dump() to print out the objects before and after you call the methods.  By comparing the objects you will be able to see the effect of the programming.

How can this be done?
0
rgb192
Asked:
rgb192
3 Solutions
 
COBOLdinosaurCommented:
echo 'before:';
var_dump($yourobject);
$yourobject->someMethod();
echo 'after:';
var_dump($yourobject);

Open in new window


Cd&
0
 
Ray PaseurCommented:
Install this and run it, then compare the output to the code.  You will see how calling the methods adds to the information inside the object.

<?php // RAY_oop_interface.php
error_reporting(E_ALL);
echo "<pre>";

// AREA FORMULAE: http://www.mathsisfun.com/area.html
define('PI', 3.1416);

// SEE http://php.net/manual/en/language.oop5.interfaces.php
interface Shapes
{
    public function rectangle($w, $h);
    public function triangle($b, $h);
    public function ellipse($a, $b);
}

class Geometry implements Shapes
{
    protected $shapes = array();

    public function rectangle($w, $h)
    {
        $shape["n"] = __FUNCTION__;
        $shape["w"] = $w;
        $shape["h"] = $h;
        $shape["area"]  = $w * $h;
        $this->shapes[] = $shape;
    }

    public function triangle($b, $h)
    {
        $shape["n"] = __FUNCTION__;
        $shape["b"] = $b;
        $shape["h"] = $h;
        $shape["area"]  = 0.5 * $b * $h;
        $this->shapes[] = $shape;
    }

    public function ellipse($a, $b)
    {
        $shape["n"] = __FUNCTION__;
        $shape["a"] = $a;
        $shape["b"] = $b;
        $shape["area"]  = PI * $a * $b;
        $this->shapes[] = $shape;
    }
}

class Area extends Geometry
{
    public function getTotalArea()
    {
        $total_area = 0.0;
        foreach($this->shapes as $shape)
        {
            $total_area += $shape['area'];
        }
        return $total_area;
    }
}

// CREATE AN INSTANCE OF THE OBJECT
$x = new Area;

// SHOW THE NEWLY INSTANTIATED OBJECT
var_dump($x);

// CALL SOME METHODS AND SHOW THE OBJECT
$x->rectangle(3,5);
$x->rectangle(2,2);
var_dump($x);

// CALL SOME MORE METHODS
$x->triangle(5,3);
$x->ellipse(1,4);

// SHOW THE OBJECT AGAIN AFTER SOME MORE WORK
var_dump($x);
echo $x->gettotalArea();

Open in new window

0
 
Slick812Commented:
The statement "Instead of using echo (which will call the __toString() method"
has absolutely NO RELEVANCE to the code in question -
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28314431.html?anchor=a39708244
as I NEVER call -
echo $at;

I never call -
echo $pep;

so Ray's comment is irrelevant to the code I wrote and only confuses  rgb192

 RAY did you even look at the code I posted? ?
the PHP code you post above has NOTHING to do with the code I post in for rgb192
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ray PaseurCommented:
@Slick812: No, I did not look at the code you posted.  PHP is a funhouse language, full of all sorts of quirky and inconsistent language constructs.  One of the things that works fairly dependably is var_dump() and that's why I like to use it.
0
 
Slick812Commented:
OK Ray, Not sure how using var_dump() will help  rgb192 understand PHP Object programming in his original question in this series, But that is in the question here. I really hope rgb192 can understand some of the non-simple example code posted here for his help. Thansk for your assistance.
0
 
rgb192Author Commented:
Cd answers question. Ray gives example about interfaces, extending and slick relates to previous question. Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now