oregon Posted December 9, 2008 Share Posted December 9, 2008 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(); ?> Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/ Share on other sites More sharing options...
gevans Posted December 9, 2008 Share Posted December 9, 2008 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(); ?> Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/#findComment-710240 Share on other sites More sharing options...
redarrow Posted December 9, 2008 Share Posted December 9, 2008 shouldn't it be //>>>>>>> public ////<<<<<<<< function showMessage() { echo $this->message."\n"; } Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/#findComment-710246 Share on other sites More sharing options...
oregon Posted December 9, 2008 Author Share Posted December 9, 2008 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? Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/#findComment-710247 Share on other sites More sharing options...
gevans Posted December 9, 2008 Share Posted December 9, 2008 @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!! Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/#findComment-710250 Share on other sites More sharing options...
oregon Posted December 9, 2008 Author Share Posted December 9, 2008 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(); ?> Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/#findComment-710253 Share on other sites More sharing options...
gevans Posted December 9, 2008 Share Posted December 9, 2008 I'm heading off now, but check out the oop tutorials here at phpfreaks Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/#findComment-710255 Share on other sites More sharing options...
oregon Posted December 9, 2008 Author Share Posted December 9, 2008 gevans, ok thanks anyhow.. i will check out those tuts. Link to comment https://forums.phpfreaks.com/topic/136170-solved-oop-construct-basic-script-i-cannot-get-to-echo-properly-whats-my-error/#findComment-710258 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.