Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Object assignment in class-problem

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Object assignment in class-problem

Postby Papapishu » Mon Sep 22, 2003 12:54 pm

I have a class, foo, and a class, bar, and in the bar class, one of the variables are initiated as an object of the class foo like this:

Code: Select all
class foo
{
  var $owner;
  function foo($x)
  {
    $this->owner=$x;
  }
}

class bar
{
  var $baz=new foo($this);
  ...
}


The idea is that foo is to be "owned" by bar.
Whenever I do this though, the script is aborted, with no error message neither, probably because the admin have removed those for the looks... :roll:
I tried error_reporting(E_ALL) though, but still get no error message.

It's not critical, but it would sure make my life, and my project, a bit better... :?
Last edited by Papapishu on Tue Sep 23, 2003 4:12 am, edited 1 time in total.
Papapishu
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sat Mar 29, 2003 12:24 pm

Postby liquedus » Mon Sep 22, 2003 6:50 pm

maybe i am seeing wrong but both your classes are foo.... 8O
liquedus
php-forum Active User
php-forum Active User
 
Posts: 266
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada

Postby Papapishu » Tue Sep 23, 2003 4:13 am

Typing mistake. :oops:
It's foo and bar now...
Papapishu
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sat Mar 29, 2003 12:24 pm

Postby Redcircle » Tue Sep 23, 2003 5:09 am

You must use a constructor function.
Code: Select all
class foo
{
  var $owner;
  function foo($x)
  {
    $this->owner=$x;
  }
}

class bar
{
  var $baz;
  function bar()
 {
    $this->baz =new foo($this);
 }
}


It's also good practice to start your class names with a capital letter.
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby Papapishu » Tue Sep 23, 2003 11:02 am

OK, it works that way, thanks for the help.
But I still can't see why it shouldn't be able to work as I wrote? :?
Papapishu
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sat Mar 29, 2003 12:24 pm

Postby swirlee » Tue Sep 23, 2003 3:15 pm

Papapishu wrote:OK, it works that way, thanks for the help.
But I still can't see why it shouldn't be able to work as I wrote? :?


$this refers to an instance of the class. Without a constructor, you can't create an instance, so you don't have access to $this. PHP doesn't have class variables, only instance variables.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Papapishu » Wed Sep 24, 2003 3:47 am

Ah, I see :)
Papapishu
New php-forum User
New php-forum User
 
Posts: 7
Joined: Sat Mar 29, 2003 12:24 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron