Re: [PHP] Help with classes (oop)

2003-02-03 Thread Chris Hayes
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;
$test=$first-first(35, chris);

print $test;

?




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Help with classes (oop)

2003-02-03 Thread Chris Boget
 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 automatically running the class' first() method.  Now, because that method 
is defined as requiring parameters, you need to do this:

$first = new first( 35, Chris );

Alternately, you can define your function/method thusly:

first($age = 0, $name = '');

but that isn't going to accomplish anything other than getting rid of the errors.
You either want to create a constructor that initializes all the member variables
or not create a constructor at all.
This would be a better way of writing your class:

class first
{
var $age;
var $name;

function first()
{
  $age = 0;
  $name = '';
}

function setData( $age, $name )
{
  $age = $age;
  $name = $name;
}

function returnData()
{
  $retval = '';

  $retval  = $this-age;
  $retval .= $this-name;

  return $retval;

}
}

Chris
(also)


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Help with classes (oop)

2003-02-03 Thread Tim Ward
the function with the same name as the class is the
constructor and is called when the instance is created.
So $first = new first runs first::first() without passing
any parameters hence the warning.

Tim Ward
http://www.chessish.com
mailto:[EMAIL PROTECTED]
- Original Message -
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.

 ? php;
 class first
 {
  var $age;
  var $name;
 
  function first($age, $name)
  {
  return $age.$name;
  }
 }
 
 //main script
 $first = new first;
 $test=$first-first(35, chris);
 
 print $test;
 ?




 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re:[PHP] Help with classes (oop)

2003-02-03 Thread Daniel Leighton
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 were instantiating the class, 
with the way you have set this up, you need to pass arguments because when you create 
a new instance of the class the function with the same name as the class is called 
and, in this case, it is expecting arguments.  Another example of a class set-up would 
be:

? php;
class first
{

function first()
{
//do something like connect to a database or set some variables
}

function second($age, $name) {
/*here you could look something up in the db or do something else and 
then return what ever you like*/
return $age.$name;
}
}

//main script
$obj = new first();
$test = $obj-second(35, chris);

print $test;
?


At 2:27 PM -0500 on 2/3/03, Leonard Burton wrote:



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 in my browser 

Warning: Missing argument 1 for first() in /usr/~index.php on
line 8
Warning: Missing argument 2 for first() in /usr/~/index.php
on line 8
35chris


As you see it does print the output.  What am I doing wrong?

Thanks,

Leonard
[EMAIL PROTECTED]

? php;
class first
{
var $age;
var $name;

function first($age, $name)
{
return $age.$name;
}
}

//main script
$first = new first;
$test=$first-first(35, chris);

print $test;


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 

Daniel Leighton
Chief Technology Officer
Webolution
http://www.webolution.com

 This email may contain material that is confidential and privileged for the
sole use of the intended recipient.  Any review, reliance or distribution
by others or forwarding without express permission is strictly prohibited.
If you are not the intended recipient, please contact the sender and delete
all copies.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Help with classes (oop)

2003-02-03 Thread Johannes Schlueter
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;
   $this-name = $name;
 }


johannes

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Help with classes (oop)

2003-02-03 Thread Chris Boget
  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 catch!  Thank you very much. :)

Chris


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Help with classes (oop)

2003-02-03 Thread Leonard Burton
Thanks for all the help thusfar.  Classes are just not something I am
understanding.  I have read the sections on classes in several books and the
www.php.net section on them and still am missing something.  It seems that
these books have been telling me what a light switch is used for but not how
to use one.

So would I call the class like this?

$first = new first;
$first-setData(35, chris);
$test=$first-returnData;
print $test;


Thanks again,

Leonard.


Just as a refresher here is the class.

class first
{
var $age;
var $name;

function first()
{
  $age = 0;
  $name = '';
}
function setData( $age, $name )
{
  $this-age = $age;
  $this-name = $name;
}

function returnData()
{
  $retval = '';

  $retval  = $this-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 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;
   $this-name = $name;
 }


johannes

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Help with classes (oop)

2003-02-03 Thread Maxim Maletsky

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 create the instance (define)
your class with already passing parameters to it. Like this:

$obj = new First($par1, $par2);

The next, you don't need to call $obj-first(..., ...); once again, as
you just did it one line above.

-- 
Maxim Maletsky
[EMAIL PROTECTED]


On Mon, 3 Feb 2003 14:27:36 -0500 Leonard Burton [EMAIL PROTECTED] wrote:

 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 in my browser 
 
 Warning: Missing argument 1 for first() in /usr/~index.php on
 line 8
 Warning: Missing argument 2 for first() in /usr/~/index.php
 on line 8
 35chris
 
 
 As you see it does print the output.  What am I doing wrong?
 
 Thanks,
 
 Leonard
 [EMAIL PROTECTED]
 
 
 
 ? php;
 class first
 {
 var $age;
 var $name;
 
 function first($age, $name)
 {
 return $age.$name;
 }
 }
 
 //main script
 $first = new first;
 $test=$first-first(35, chris);
 
 print $test;
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] help with classes

2001-02-13 Thread Marcus Rasmussen


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 SEPARATOR  ***

On 14-02-2001 at 15:52 Joseph H Blythe wrote:

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 ?PHP
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 echo "htmlbody";
JC 
JC $cls = new foo();
JC $cls-mymethod();
JC echo $cls-bar;
JC 
JC echo "/body/html";
JC ?
JC 

hmm sort of is, basically this works:

// Foo.class.php
class Foo {

var $bar;

setBar($bar) {
   $this-bar = $bar;
}

}

// foo.php
include("Foo.class.php");
$foo = new Foo;
$bar = "1234567789";
$foo-setBar($bar);

Regards,

Joseph

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] help with classes

2001-02-13 Thread Jesse Swensen

on 2/13/01 12:04 AM, Joe Conway at [EMAIL PROTECTED] wrote:

 ?PHP
 
 class Foo {
 
 var $bar;
 
 function mymethod(){
 
 global $foo;
 $this-bar = $foo;
 
 }
 
 }
 
 $foo = "hello world";
 
 echo "htmlbody";
 
 $cls = new foo();
 $cls-mymethod();
 echo $cls-bar;
 
 echo "/body/html";
 ?

This can be simplified by:

?php
class Foo {
function Foo($foo="") {
$this-bar = $foo;
}
}

$foo = "Hello World";
$cls = new Foo($foo);
echo $cls-bar;
?
-- 
Jesse Swensen
[EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] help with classes

2001-02-12 Thread Joe Conway

 Was just wondering how to do the following:

 ?php
 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
withot doing something like:

 var $bar = '';
 $this-bar = $foo;

 Any insight would be much appreciated.

I was curious too, so I looked it up. Seems you can't. From
http://www.php.net/manual/en/language.oop.php

"Note: In PHP 4, only constant initializers for var variables are
allowed. Use constructors for non-constant initializers."

Hope this helps,

Joe




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] help with classes

2001-02-12 Thread Joseph H Blythe

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 initializers."
JC 
JC Hope this helps,
JC 
JC Joe
JC 

I missed that one little cryptic note :-) 

I basically just made a function that sets the var variable to what I wanted. Unless 
*anyone* knows a better way?

class Foo {
var $bar;

function setBar($bar) {
  this-bar = $bar;
}

}

Thanks for your help!

Regards,


Joseph



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] help with classes

2001-02-12 Thread Sean Cazzell

 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 () {
$this-bar = $foo;
}
}


Regards,

Sean


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] help with classes

2001-02-12 Thread Joseph H Blythe

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 routines like perl? shouldn't that be:

function MyClass(){
  $this-bar = $foo;
}

I tried this and it does not evaluate the variable. The only way I could get this to 
work was to create a function that I can pass the variable to as one of it paramaters 
(see previous post)

I could be wrong?


Regards


Joseph

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] help with classes

2001-02-12 Thread Joe Conway

 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(){
   $this-bar = $foo;
 }

 I tried this and it does not evaluate the variable. The only way I could
get this to work was to create a function that I can pass the variable to as
one of it paramaters (see previous post)

 I could be wrong?



Is this closer to what you were looking for?
- Joe

?PHP

 class Foo {

 var $bar;

 function mymethod(){

  global $foo;
  $this-bar = $foo;

 }

}

$foo = "hello world";

echo "htmlbody";

$cls = new foo();
$cls-mymethod();
echo $cls-bar;

echo "/body/html";
?


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] help with classes

2001-02-12 Thread Ankur Verma

you do this in the class constructor

class foo
{

  var $bar;

  function foo($fooval)
 {
$this-bar=$fooval;
 }

}

hope that helps

Ankur Verma
HCL Technologies
A1CD, Sec -16
Noida, UP
India

- Original Message -
From: "Joseph H Blythe" [EMAIL PROTECTED]
To: "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:

 ?php
 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
withot doing something like:

 var $bar = '';
 $this-bar = $foo;

 Any insight would be much appreciated.

 Regards

 Joseph

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]