Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-10
6
Medium Priority
?
357 Views
Last Modified: 2013-12-11
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
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 668 total points
ID: 39709295
echo 'before:';
var_dump($yourobject);
$yourobject->someMethod();
echo 'after:';
var_dump($yourobject);

Open in new window


Cd&
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 39709310
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
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 668 total points
ID: 39709451
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39709491
@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
 
LVL 34

Expert Comment

by:Slick812
ID: 39710365
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
 

Author Closing Comment

by:rgb192
ID: 39712384
Cd answers question. Ray gives example about interfaces, extending and slick relates to previous question. Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question