Jump to content

Recommended Posts

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;

}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656622
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656623
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656637
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/126936-solved-new-to-oop/#findComment-656775
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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