Solved

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

Posted on 2013-12-10
6
344 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 167 total points
ID: 39709295
echo 'before:';
var_dump($yourobject);
$yourobject->someMethod();
echo 'after:';
var_dump($yourobject);

Open in new window


Cd&
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 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 167 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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