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 crea
his->age;
$retval .= $this->name;
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 2
> > 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;
>
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;
Hi Leonard,
Try 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 were instantiating the class,
with the way you have set this up, you need to pass arguments because when you create
-
From: 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.
>
> > &
> 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 auto
Apparently it does not like the function name to be the same as the class
name. So change one of them.
first(35, "chris");
print $test;
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
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
on 2/13/01 12:04 AM, Joe Conway at [EMAIL PROTECTED] wrote:
>
> class Foo {
>
> var $bar;
>
> function mymethod(){
>
> global $foo;
> $this->bar = $foo;
>
> }
>
> }
>
> $foo = "hello world";
>
> echo "";
>
> $cls = new foo();
> $cls->mymethod();
> echo $cls->bar;
>
> echo "";
> ?>
Thi
You can allso do this:
//foo.class.php
class Foo{
var $bar;
function Foo($bar){
$this->bar = $bar;
}
}
//foo.php
include("foo.class.php");
$bar = "1234";
$foo = new Foo($bar);
//Now: $foo->bar = "1234"
Regards: Marcus Rasmussen
*** REPLY SEPARAT
uot;php-general" <[EMAIL PROTECTED]>
Sent: Wednesday, February 14, 2001 9:27 AM
Subject: [PHP] help with classes
> hey all,
>
> Was just wondering how to do the following:
>
> class Foo {
>
> var $bar = $foo; // causes parse error
> var $bar = "$foo"
On Mon, 12 Feb 2001 21:04:30 -0800
Joe wrote:
JC> Is this closer to what you were looking for?
JC> - Joe
JC>
JC>
JC> class Foo {
JC>
JC> var $bar;
JC>
JC> function mymethod(){
JC>
JC> global $foo;
JC> $this->bar = $foo;
JC>
JC> }
JC>
JC> }
JC>
JC> $foo = "hello world";
JC>
JC> e
> SC>
> SC> That's how you have to do it.
> SC>
> SC>
> SC> class MyClass {
> SC> var $bar;
> SC>
> SC> // This is the class's constructor
> SC> sub MyClass () {
> SC> $this->bar = $foo;
> SC> }
> SC> }
>
> I didn't think php had sub routines like perl? shouldn't that be:
>
> function MyClass(){
>
On Mon, 12 Feb 2001 23:44:08 -0500 (EST)
Sean wrote:
SC>
SC> That's how you have to do it.
SC>
SC>
SC> class MyClass {
SC> var $bar;
SC>
SC> // This is the class's constructor
SC> sub MyClass () {
SC> $this->bar = $foo;
SC> }
SC> }
I didn't think php had sub routi
> So how does one correctly assign a variable to a variable inside a class withot
>doing something like:
>
> var $bar = '';
> $this->bar = $foo;
>
That's how you have to do it.
class MyClass {
var $bar;
// This is the class's constructor
sub MyClass () {
On Mon, 12 Feb 2001 20:11:14 -0800
Joe wrote:
JC>
JC> I was curious too, so I looked it up. Seems you can't. From
JC> http://www.php.net/manual/en/language.oop.php
JC>
JC> "Note: In PHP 4, only constant initializers for var variables are
JC> allowed. Use constructors for non-constant initia
> Was just wondering how to do the following:
>
> class Foo {
>
> var $bar = $foo; // causes parse error
> var $bar = "$foo"; // causes parse error
> var $bar = '$foo'; // works but $foo is not evaluated
>
> }
> ?>
>
> So how does one correctly assign a variable to a variable inside a class
witho
hey all,
Was just wondering how to do the following:
So how does one correctly assign a variable to a variable inside a class withot doing
something like:
var $bar = '';
$this->bar = $foo;
Any insight would be much appreciated.
Regards
Joseph
--
PHP General Mailing List (http://www.php.
19 matches
Mail list logo