Archadian Posted October 3, 2008 Share Posted October 3, 2008 I am getting this error and don't know why. I am new to OOP and trying to learn the language. If anyone has any tips or some easy way to learn it would be very much appreciated. Here is the error: Warning: Missing argument 1 for Message::Message(), called in F:\Inetpub\wwwroot\ooptest.php on line 15 and defined in F:\Inetpub\wwwroot\oop.php on line 8 And here is the code: ooptest.php <html> <head> <title>The Cabal Online Guide</title> </head> <body> <?php include('oop.php'); $message = "Test Message!"; //Line 15 $mess =& new Message; $mess->body($message); $see = $mess->body(); echo "<table align=\"center\" valign=\"middle\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" height=\"45\">"; echo "<tr>"; echo "<td align=\"center\" width=\"589\" height=\"45\" border=\"0\">" . $see . "</td>"; echo "</tr>"; echo "</table>"; ?> </body></html> oop.php <?php class Message { var $message = ''; function Message($body){ //Line 8 $this->message = $body; } function body(){ return $this->message; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/ Share on other sites More sharing options...
Archadian Posted October 3, 2008 Author Share Posted October 3, 2008 No one knows what is wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656618 Share on other sites More sharing options...
KevinM1 Posted October 3, 2008 Share Posted October 3, 2008 Well, there are several things at play here. First, you don't initialize your object correctly. You should write: $mess =& new Message($message); Why? Because of your constructor: function Message($body){ //Line 8 $this->message = $body; } Since you defined a constructor for your object, it's expecting you to pass to it the one parameter defined in its signature ($body). Even if you didn't define your own constructor, you'd still need to instantiate the object like so: $mess =& new Message(); Object creation always invokes an object constructor, which, in turn, is a function. Since it's a function, you need the parentheses. The next line follows the same train of thought from the previous mistake. Your body() function merely returns a value. It doesn't set the $message member of the object. So, trying to use it in that context doesn't make sense. Instead, you'll need to write what's commonly called a 'setter' function to go along with your 'getter' function. Together, they'd look something like this: function getMessage() { return $this->message; } function setMessage($message) { $this->message = $message; } Using these within the client code would look like: $myMessage = "Welcome to Rapture"; $welcome =& new Message($myMessage); echo "$welcome->getMessage()"; $welcome->setMessage("Don't let the Big Daddy's get you"); echo "$welcome->getMessage()"; Finally, if you're serious about trying OOP with PHP, you should really update to PHP 5. I cannot stress how much better its OOP capabilities are. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656622 Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 Where did you get this OOP code, might I ask? It's using old, PHP4 OOP syntax. I'll explain why it errored out after, but for now, change your class to: <?php class Message { protected $message; public function __construct($message) { $this->setMessage($message); } public function getMessage() { return $this->message; } public function setMessage($message) { if (strlen(trim($message)) == 0) { throw new Exception('Blank message given.'); } $this->message = $message; } } ?> I added some new things in there, but hopefully you get the idea and can learn a bit. Now, change your main file to: <html> <head> <title>The Cabal Online Guide</title> </head> <body> <?php include('oop.php'); $message = "Test Message!"; $mess = new Message($message); //pass in $message in the constructor!!! $see = $mess->getMessage(); echo "<table align=\"center\" valign=\"middle\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" height=\"45\">"; echo "<tr>"; echo "<td align=\"center\" width=\"589\" height=\"45\" border=\"0\">" . $see . "</td>"; echo "</tr>"; echo "</table>"; ?> </body></html> Tell me if those changes were confusing. The reason your code was failing was because you defined an argument ($message) in the function header for the constructor, but you never passed one in. And yeah, where are you getting this OOP code? You'll want to learn how to use the new, better OOP system. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656623 Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 @Nightslyr: Actually, if an object's constructor takes no parameters, you can leave off the parens. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656624 Share on other sites More sharing options...
KevinM1 Posted October 3, 2008 Share Posted October 3, 2008 @Nightslyr: Actually, if an object's constructor takes no parameters, you can leave off the parens. Oh, really? Interesting. Eh, I still like the parens...it matches the rest of my coding style. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656629 Share on other sites More sharing options...
Archadian Posted October 3, 2008 Author Share Posted October 3, 2008 I have php 5 installed. I am just new to OOP so im not familiar with anything yet. Functions i can do in PHP...OOP is a work in progress. I actually wrote this myself...didn't find it anywhere...just a simple OOP code. I am a "hands on" learner so i have to write code and do the whole trial and error thing. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656637 Share on other sites More sharing options...
Archadian Posted October 3, 2008 Author Share Posted October 3, 2008 Also...any online tutorials on the new OOP (PHP 5) syntax? Or any books I can buy? Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656645 Share on other sites More sharing options...
xtopolis Posted October 4, 2008 Share Posted October 4, 2008 http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect http://www.phpfreaks.com/tutorial/oo-php-part-2-boring-oo-principles http://www.phpfreaks.com/tutorial/oo-php-part-3-uml-classes-and-relations Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656726 Share on other sites More sharing options...
Archadian Posted October 4, 2008 Author Share Posted October 4, 2008 I have another question for learning purposes. Can anyone tell me what is wrong with this code? Hopefully someone can help me out. Thanks in advance. class.php class Mysql { protected $host = ''; protected $user = ''; protected $pass = ''; protected $result; protected $db; public function __constructor($host, $user, $pass, $db) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->result = $result; $this->db = $db; } public function web() { mysql_connect($this->host, $this->user, $this->pass) or die("Connection to the website DB failed."); $this->result = mysql_select_db('db') or die("Selecting the DB failed."); return $this->result; } public function forums() { mysql_connect($this->host, $this->user, $this->pass) or die("Connection to the forums DB failed."); $this->result = mysql_select_db('db') or die("Selecting the DB failed."); return $this->result; } } and here is the code on the index.php $db =& new Mysql($host, $user, $pass); $this->web(); I know its wrong so if someone could help me out and let me know what i need to do or change i would appreciate it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656775 Share on other sites More sharing options...
xtopolis Posted October 4, 2008 Share Posted October 4, 2008 You need to make it say: $db->web(); "this" should only be used inside a class, afaik Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656779 Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 If you wish to pass a blank argument you can do so by setting it in the constructor. public function __constructor($host, $user, $pass, $db="") Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656781 Share on other sites More sharing options...
DarkWater Posted October 4, 2008 Share Posted October 4, 2008 It should be __construct, not __constructor. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656822 Share on other sites More sharing options...
JasonLewis Posted October 4, 2008 Share Posted October 4, 2008 Ha! Indeed it should. Didn't even notice that one. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656823 Share on other sites More sharing options...
Archadian Posted October 4, 2008 Author Share Posted October 4, 2008 __construct doesn't work. on the other hand __constructor works great Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656829 Share on other sites More sharing options...
DarkWater Posted October 4, 2008 Share Posted October 4, 2008 __construct doesn't work. on the other hand __constructor works great __construct is called automatically when a class is created, trust me. Quote Link to comment https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656936 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.