Author Topic: [SOLVED] New to OOP  (Read 928 times)

0 Members and 1 Guest are viewing this topic.

Offline ArchadianTopic starter

  • Enthusiast
  • Posts: 224
    • View Profile
[SOLVED] New to OOP
« on: October 03, 2008, 04:14:35 PM »
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:

Quote
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
Code: [Select]
<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
Code: [Select]
<?php

class Message
{

var $message '';

function Message($body){  //Line 8

$this->message $body;

}

function body(){

return $this->message;

}

}

?>
« Last Edit: October 03, 2008, 04:16:02 PM by Archadian »

Offline ArchadianTopic starter

  • Enthusiast
  • Posts: 224
    • View Profile
Re: New to OOP
« Reply #1 on: October 03, 2008, 05:05:36 PM »
No one knows what is wrong here?
« Last Edit: October 03, 2008, 05:06:56 PM by Archadian »

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 3,909
  • Gender: Male
    • View Profile
Re: New to OOP
« Reply #2 on: October 03, 2008, 05:10:42 PM »
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.
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: New to OOP
« Reply #3 on: October 03, 2008, 05:12:23 PM »
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:

Code: [Select]
<?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:
Code: [Select]
<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.
« Last Edit: October 03, 2008, 05:13:22 PM by DarkWater »
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: New to OOP
« Reply #4 on: October 03, 2008, 05:15:04 PM »
@Nightslyr: Actually, if an object's constructor takes no parameters, you can leave off the parens.
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Online KevinM1

  • Global Moderator
  • Fanatic
  • *
  • Posts: 3,909
  • Gender: Male
    • View Profile
Re: New to OOP
« Reply #5 on: October 03, 2008, 05:23:09 PM »
@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. ;)
Don't go to w3schools.  Here's why
Every time a PHP coder uses 'global' a kitten dies.

Offline ArchadianTopic starter

  • Enthusiast
  • Posts: 224
    • View Profile
Re: New to OOP
« Reply #6 on: October 03, 2008, 05:31:26 PM »
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.
« Last Edit: October 03, 2008, 05:37:45 PM by Archadian »

Offline ArchadianTopic starter

  • Enthusiast
  • Posts: 224
    • View Profile
Re: New to OOP
« Reply #7 on: October 03, 2008, 05:40:26 PM »
Also...any online tutorials on the new OOP (PHP 5) syntax? Or any books I can buy?
« Last Edit: October 03, 2008, 05:46:20 PM by Archadian »


Offline ArchadianTopic starter

  • Enthusiast
  • Posts: 224
    • View Profile
Re: New to OOP
« Reply #9 on: October 03, 2008, 09:34:39 PM »
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
Code: [Select]
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

Code: [Select]
$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.
« Last Edit: October 03, 2008, 09:37:24 PM by Archadian »

Offline xtopolis

  • Devotee
  • Posts: 1,422
  • Gender: Male
  • [Can't stop. Won't stop.]
    • View Profile
Re: New to OOP
« Reply #10 on: October 03, 2008, 09:43:28 PM »
You need to make it say:

$db->web();

"this" should only be used inside a class, afaik
« Last Edit: October 03, 2008, 09:45:40 PM by xtopolis »

Offline Jaysonic

  • Fanatic
  • Posts: 3,332
  • Gender: Male
  • 8548863
    • View Profile
    • I Am Lewy Babes
Re: New to OOP
« Reply #11 on: October 03, 2008, 09:45:34 PM »
If you wish to pass a blank argument you can do so by setting it in the constructor.

Code: [Select]
public function __constructor($host, $user, $pass, $db="")
Good luck with your coding.
Jason ~ ProjectFear ~ Jaysonic (and variations of... :))

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: [SOLVED] New to OOP
« Reply #12 on: October 03, 2008, 11:35:51 PM »
It should be __construct, not __constructor.
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline Jaysonic

  • Fanatic
  • Posts: 3,332
  • Gender: Male
  • 8548863
    • View Profile
    • I Am Lewy Babes
Re: [SOLVED] New to OOP
« Reply #13 on: October 03, 2008, 11:36:41 PM »
Ha! Indeed it should. Didn't even notice that one.
Good luck with your coding.
Jason ~ ProjectFear ~ Jaysonic (and variations of... :))

Offline ArchadianTopic starter

  • Enthusiast
  • Posts: 224
    • View Profile
Re: [SOLVED] New to OOP
« Reply #14 on: October 03, 2008, 11:39:41 PM »
__construct doesn't work. on the other hand __constructor works great

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: [SOLVED] New to OOP
« Reply #15 on: October 04, 2008, 06:57:37 AM »
__construct doesn't work. on the other hand __constructor works great

__construct is called automatically when a class is created, trust me.
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.