Jump to content

Outputting Object Contents


TomTees

Recommended Posts

What is the proper way to output the contents of a class?

 

(We will assume that Getters and Setters are evil...)

 

Let's say I have the following class...

 

class FormHandler2 {

// Define Variables.

private $myFormArray;

 

// Constructor.

public function __construct($param){

$this->myFormArray = $param;

}

}

 

...and I want to be able to output its contents either via a variable dump OR by printing something to the screen.

 

How would I do that?

 

 

 

TomTees

 

 

Link to comment
Share on other sites

Well firstly you are using php5 engine class instructions (public, private, protected etc), and these will not work if you try it on a server with php<5 on it.

 

In answer to your question, classes and their usage is what makes them great and versatile; functions that you want to use outside the class, you would name as public or static (access static by using the scope resolution operator :: ), or just (->)pointing to them, or properties of the methods anyway (so long as they are set (have state))

 

You don't really 'output' a class to screen, it's lots of little sub procedures and methods that all interlace to make something happen, other than that I can't really suggest anything else that could be helpful.

$this->error_message = false;

public function __construct($param){
       if(file_exists(BASE_DIR."Controls.php")){  
                $this->myFormArray = $param;
       }
       else{
              $this->error_message = true;
       }
}

 

This is my favourite way of doing my constants, import them all at the same time, this makes it really handy for doing il8n when you want to do language packs to make your site more accessible...

 

I think as you just need to sit and play for a while... That's how I learned, read the details, then asked questions if I was unsure..

 

Have fun.

 

Rw

Link to comment
Share on other sites

print_r($myObject)  will output the objects properties (interval variables) to the screen.

 

 

OOps, I realise now that I totally misunderstood your question :facepalm: Yep, the182guy's advice will do exactly that, you can optionally place an exit after that call so that you don't get any 'interference' from any other part of the class that the object/property/function may be accessing.

 

You can also use echo's and they will be printed to the very top left of the screen.

 

Rw

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.