Jump to content

problem with inheritance in classes


leeming

Recommended Posts

im using PHP Version 4.4.2 and cant figgure out inheritance in classes...
I have read a few tutorials on how to do this, but it still isnt working

eg..
[code]
class user
{
  var $userid = USERID;
  ...
  ...
  ...
}

class staff extends user
{
  ...
  ...
}[/code]

if userid is set in the user class... using $this->userid in the staff class doesnt work (why not?)
but i tried some thing along the lines of a function that gets the userid (in the user class)
and useing 'user::getuserid()' works... but surely these values should be inherited any way...

i have tried $perant->userid but that doesnt work either.
Link to comment
https://forums.phpfreaks.com/topic/31275-problem-with-inheritance-in-classes/
Share on other sites

[quote author=thorpe link=topic=119290.msg488523#msg488523 date=1166568655]
Is USERID hard coded into the user class?
[/quote]

USERID is a constant inside the whole of the code (do they not work inside classes? do i have to global them)

but if i try to print $user->userid it works, but not as the inherited staff->userid

i am very new to classes, and want to learn as much as i can about them, and get in the habit of using them, as im working upto PC programming also


~edit~
i think i might be calling the class wrong?
if i call.
[code]
$user = new user;
$user->userid = USERID;

...
...
$staff = new staff;
[/code]

im thinking the code KNOWS that the $user values are connected to $staff (the classes are, but nothing to say there could be $user1 and $user2 using the user class?)

~edit2~
i think i have it now, after typing the 1st edit note... realised that all i have to do is call the staff class, and not user then staff... ;)
If I run this, it outputs 'abcde'

[code]
<?php
define ("USERID", 'abcde');

class user
{
  var $userid = USERID;
 
}

class staff extends user
{
  function print_user()  {
    echo $this->userid;
  }
}

$obj = new staff;
$obj->print_user();        // --> abcde
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.