Greetings,
I am trying to figure out using classes. I have read and read and read
about them but still cannot figure them new fangled things out. Could
someone please alter this snippet just a bit so it would be a correct test
script with a call to it?
When I run the script I get this
Apparently it does not like the function name to be the same as the class
name. So change one of them.
? php;
class first
{
var $age;
var $name;
function first($age, $name)
{
return $age.$name;
}
}
//main script
$first = new first;
Apparently it does not like the function name to be the same as the class
name. So change one of them.
No, what's happening is that when you instantiate an class, it runs
(as a constructor) the function whose name is the same as the class.
So when you do this:
$first = new first;
it's
: Chris Hayes [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Monday, February 03, 2003 7:35 PM
Subject: Re: [PHP] Help with classes (oop)
Apparently it does not like the function name to be the same as the class
name. So change one of them.
? php;
class first
{
var $age;
var
Hi Leonard,
Try this:
? php;
class first
{
var $total;
function first($age, $name)
{
$this-total = $age.$name;
}
}
//main script
$obj = new first(35, chris);
print $obj-total;
?
The problem with what you were doing is that when you
On Monday 03 February 2003 20:45, Chris Boget wrote:
function setData( $age, $name )
{
$age = $age;
$name = $name;
}
Is useless ;-) I think you wanted this:
function setData( $age, $name )
{
$this-age = $age;
function setData( $age, $name )
{
$age = $age;
$name = $name;
}
Is useless ;-) I think you wanted this:
function setData( $age, $name )
{
$this-age = $age;
$this-name = $name;
}
Good
;
return $retval;
}
}
-Original Message-
From: Johannes Schlueter [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 03, 2003 2:59 PM
To: [EMAIL PROTECTED]
Subject: Re: [PHP] Help with classes (oop)
On Monday 03 February 2003 20:45, Chris Boget wrote:
function setData
When you name a function in the class with the same name as the class
itself, this function gets automatically executed upon defining the
object (class). this is called `constructor'.
in your very case, this is the function first, which requires two
parameters to be passed to it. You need to
9 matches
Mail list logo