Solved

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

Posted on 2013-12-10
6
336 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
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 109

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 109

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

820 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