Jump to content

OOP "Lang" Class


Ballam
Go to solution Solved by requinix,

Recommended Posts

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 :)

Link to comment
Share on other sites

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.