leeming Posted December 19, 2006 Share Posted December 19, 2006 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 workingeg..[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. Quote Link to comment https://forums.phpfreaks.com/topic/31275-problem-with-inheritance-in-classes/ Share on other sites More sharing options...
trq Posted December 19, 2006 Share Posted December 19, 2006 Is USERID hard coded into the user class? Quote Link to comment https://forums.phpfreaks.com/topic/31275-problem-with-inheritance-in-classes/#findComment-144792 Share on other sites More sharing options...
leeming Posted December 20, 2006 Author Share Posted December 20, 2006 [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->useridi 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... ;) Quote Link to comment https://forums.phpfreaks.com/topic/31275-problem-with-inheritance-in-classes/#findComment-145472 Share on other sites More sharing options...
Barand Posted December 21, 2006 Share Posted December 21, 2006 If I run this, it outputs 'abcde'[code]<?phpdefine ("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] Quote Link to comment https://forums.phpfreaks.com/topic/31275-problem-with-inheritance-in-classes/#findComment-145583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.