Jump to content

[SOLVED] oop construct basic script i cannot get to echo properly - what's my error!


oregon

Recommended Posts

im toying around with php constructs, and i cannot get this one to echo properly. i also continue to get php error:

 

Warning: Missing argument 1 for MessageMan::__construct(), called in \htdocs\jasons\php\5.php on line 18 and defined in \htdocs\php\5.php on line 8

 

<?php

$message = "This is the message";

class MessageMan
{
private $message;
function __construct($message)
{
	$this->message = $message;
}
function showMessage()
{
	echo $this->message."\n";
}
}

$new_message = new MessageMan;
$new_message->showMessage();

?>

Right, lets have a look.

 

When you assign your object $new_message = new MessageMan; you need to pass a variable to your constructor function, so;

 

$new_message = new MessageMan($message);

 

In full;

 

<?php

$message = "This is the message";

class MessageMan
{
   private $message;
   function __construct($message)
   {
      $this->message = $message;
   }
   function showMessage()
   {
      echo $this->message."\n";
   }
}

$new_message = new MessageMan($message);
$new_message->showMessage();

?>

gevans,

of course you would be the first to get this one right! nice.

 

i am curious however. so the basic purpose of the __constructor is to have a default predetermined value for an object when it is created? am i getting this right?

 

for example, if the object was a Car, i would set a constructor for the color let's say in red, and any time i initialized the object Car, it would be red?

 

am i tracking or totally lost?

@redarrow

Yes it should be, already discussed in another thread :)

 

For the moment I thought I'd just fix and advise on the problem!

 

@oregon

You're on the right lines, it comes into play more once your classes have some more functionality.

 

An example would be a class used for database function, you could use the _construct function to connect to the database so on creating a new onbject the connection is made.

 

You could follow this by using the __destruct function to close the connection!!

gevans,

 

i see what you mean.

 

could you possibly post up the db connection class you mentioned? and a simple example of it in use?

 

i continued to mess with that other script and took it to a new level (added 2 messages). hey it actually works:

 

<?php
$message = "This is the message";
$message2 = "This is the rest of it!";
class MessageMan
{
private $message;
private $message2;
function __construct($message,$message2)
{
	$this->message = $message;
	$this->message2 = $message2;
}
function showMessage()
{
	echo $this->message; 
	echo $this->message2;
}
}
$new_message = new MessageMan($message, $message2); 
$new_message->showMessage();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.