0

i've got a bit of a problem with getting a singleton to work in php 5.3

What i want to achieve is that i'll be able to include one php file with a class, that lets me translate webpages by a dictionary over a global singleton.

Usage example:

<?php
    include_once "CLocale.php";
?>
//...
<head>
<title><?php CLocale::Instance()->getText("StrMemberArea")?></title>

My class looks like the following at the moment:

class CLocale
{
    private function __clone()
    {
    }

    public static function Instance()
    {
        if (static::$_instance === NULL)
        {
            static::$_instance = new static();
        }
        return static::$_instance;
    }

    private function __construct()
    {
    }

    public function getText($str)
    {
        return "Test";
    }
}

So, the problem is, i don't get any output of "Test" when using the class like shown above, also, i don't get any error. PHP Storm isn't really showing me any errors. Perhaps one of you guys is able to spot a problem somewhere.

Thanks in advance, calav3ra

PS: I don't mind how the singleton is implemented

4
  • 1
    You're returning it. But you're not passing it to output, as there is no echo call. Commented Feb 11, 2013 at 12:19
  • 1
    Thanks, geremy/nslbshtr. I cannot believe what i did. And i wasted three hours before asking here. Thanks a lot, again! :) Commented Feb 11, 2013 at 12:48
  • 1
    Sometimes you just need another pair of eyes to look into your code, to see something obvious:) Commented Feb 11, 2013 at 12:49
  • I know. Actually, i didn't sleep enough, but still feel ok - but it's always the same :) Commented Feb 11, 2013 at 13:12

3 Answers 3

3

Yo forgot to echo the result

<title><?php CLocale::Instance()->getText("StrMemberArea")?></title>

should be:

<title><?php echo CLocale::Instance()->getText("StrMemberArea")?></title>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot PeterM, the same applies to your answer as to . I cannot believe what i did. :)
2

Ehm - the Singleton code is completely right, but you forgot to output the return value from getText

<?php
    include_once "CLocale.php";
?>
//...
<head>
<title><?php echo CLocale::Instance()->getText("StrMemberArea")?></title>

1 Comment

Can't state it often enough: What did i think when not echoing at all?? Thanks, Philiüü
2

To get anything displayed you no just need to return it, but also echo or print it. Like this:

<title><?php echo CLocale::Instance()->getText("StrMemberArea")?></title>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.