Solved

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

Posted on 2013-12-10
6
321 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 108

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 33

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 33

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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 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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now