Ballam Posted August 14, 2013 Share Posted August 14, 2013 Good evening all! I have been working on a project for some time, and as it is now getting quite large I am now trying to extract the textual parts into an object called Lang so that it's easier to read the code, and edit the text. This works for more simple text sections, however the issue I am having is in text where I want to pull the users name from another object, i'm not sure how to do this without actually passing the user object to the lang object, however I don't want to do this as I don't feel it is correct? Below is an extract example to show how I am trying to implement this: /**** In the lang.php file ****/ class Lang { private static $lang = array(); function __construct() { $lang['WELCOME'] = "Hello " . $user->getRealname() . "<br /> <br />Welcome to the Control Panel.<br /><br /> Please pick an option from the menu on the left.<br /><br /> <a href='?action=logout'>Please click here to Logout</a>"; } public function getText($id) { return $this->lang[$id]; } /**** In the actual site code ****/ require('classes/user.php'); $user = new User($db); require('classes/lang.php'); $lang = new Lang(); $content = $lang->getText('WELCOME'); Obviously the above code is shortened to just show the key parts. The code is encountering a fatal error due to the $user->getRealName() method call, and i'm not sure of the easiest way to resolve this. Please could somebody provide their thoughts and advice? Error:Fatal error: Call to a member function getRealname() on a non-object in /public_html/dev/classes/lang.php on line 6 Thank you for reading! B Quote Link to comment https://forums.phpfreaks.com/topic/281180-oop-lang-class/ Share on other sites More sharing options...
Solution requinix Posted August 14, 2013 Solution Share Posted August 14, 2013 Pass $user into Lang's constructor as an argument. Quote Link to comment https://forums.phpfreaks.com/topic/281180-oop-lang-class/#findComment-1445079 Share on other sites More sharing options...
Ballam Posted August 14, 2013 Author Share Posted August 14, 2013 (edited) Thank you, I did consider this but wasn't sure whether it was the best course of action. Implemented now and the error has gone. Thanks again Edited August 14, 2013 by Ballam Quote Link to comment https://forums.phpfreaks.com/topic/281180-oop-lang-class/#findComment-1445082 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.