Solved

OOP Programming Beginner - How do I make this class work?

Posted on 2014-01-28
4
298 Views
Last Modified: 2014-01-28
I'm strating at the beginning and trying to get my brain around OOP. I've got an "OOP for Dummies" in front of me and going through it one exercise at a time.

There's one example that they use and while they use it to illustrate the scaffolding of a class, they don't show how it would actually be used to generate a message, and I want to figure that out.

Here's my class:

class MessageHandler {
      private $message;
      function _construct($message)
      {
            $this->message = $message;
      }
      function displayMessage()
      {
      echo $this->message."/n";
      }
}

In my HTML, I start by instantiating the class...

$the_message = new MessageHandler;

...and then, that's where it seems to stop.

If I wanted to say "Welcome to the wonderful world of OOP!" how would I do that using my class?
0
Comment
Question by:brucegust
  • 3
4 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39815338
$thing = new MessageHandler('wELCOME...');
$thing->DisplayMessage();
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39815351
Darn it, Caps Lock.  The most useless key on the keyboard.  But that aside, the example is still structurally correct.

However I would check the date and quality of the learning resource you're using.  In PHP OOP, you can and should designate the visibility of the methods (methods == functions).

Our colleague gr8gonzo has these articles to help you get going:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2626-Beginning-Object-Oriented-Programming-in-PHP.html

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2631-Advanced-Object-Oriented-Programming-in-PHP.html
0
 

Author Closing Comment

by:brucegust
ID: 39815506
Thanks, Ray!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39815802
Glad to help.  You'll have more OOP questions, I'm sure.  We will be here! ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now