TomTees Posted September 27, 2010 Share Posted September 27, 2010 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 Quote Link to comment Share on other sites More sharing options...
rwwd Posted September 27, 2010 Share Posted September 27, 2010 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 Quote Link to comment Share on other sites More sharing options...
the182guy Posted September 27, 2010 Share Posted September 27, 2010 print_r($myObject) will output the objects properties (interval variables) to the screen. Quote Link to comment Share on other sites More sharing options...
rwwd Posted September 27, 2010 Share Posted September 27, 2010 print_r($myObject) will output the objects properties (interval variables) to the screen. OOps, I realise now that I totally misunderstood your question 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.