Jump to content

PHP (Classes): "Missing argument 1 for myclass"


Recommended Posts

a PHP n00b here, :unsure: having a little trouble, and can not seem to understand why this message is generating:

Warning: Missing argument 1 for myclass() in C:\Program Files\xampp\htdocs\class.php on line 5

1:  <?php
2:
3: class myClass
4: {
5: function myClass( $x )
6: {
7: $this->setName( $x );
8: }
9:
10: function setName( $n )
11: {
12: $this->name = $n;
13: }
14:
15: function getName()
16: {
17: return $this->name;
18: }
19:
20: function displayMessage()
21: {
22: echo "Hello! My name is ".$this->getName().'.';
23: }
24:
25: var $name = "Matt";
26: }
27:
28: $mc = new myClass();
29: $mc->displayMessage();
30:
31: echo "<br>";
32: $mc->setName( "Julie" );
33: $mc->displayMessage();
34:
35: ?>

Full output was:

Warning: Missing argument 1 for myclass() in C:\Program Files\xampp\htdocs\class.php on line 5

Hello! My name is .

Hello! My name is Julie.

Thanks :)

Link to comment
Share on other sites


well, for those who come across this later...

it's because he instantiated the class, but the constructor wanted an argument and he didn't specify a default one.

one easy fix would be

function myClass( $x = "" )

That way, if you don't specify an argument to pass, it SHOULD then see that it's defined in the function already.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...