Jump to content

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.