1

My brain is running out of power and with it my skill to search (I didn't find an 100% answer to what I'm looking for). That being said, I have a class inside which is an empty static variable $title which I'd need to define inside __construct, but it can't seem to get it via global. Let's just show some code, shall we?

$arg = array( my data is here );
class Article
{
    static $title;

    public function __construct()
    {
        global $arg;
        self::$title = $arg['title'];
    }

    public static function title()
    {
        return self::$title;
    }
}

My end goal would be to simply do

echo Article::title();

to get the title of the article. But as I previously mentioned, I have no such luck. All help is appreciated.

6
  • 4
    Why do you want this field to be static? Why can't it be a regular class attribute? Also, why are you using global rather than just passing the title to the constructor? Commented Apr 10, 2012 at 1:24
  • So I wouldn't have to manually init. the class every time I use it. It's way prettier for the user if it stays as Article::. Commented Apr 10, 2012 at 1:25
  • 2
    I'm not sure you understand the usage of classes and class instances. If you only need one copy of a class, just create a single instance of the class and use it. Commented Apr 10, 2012 at 1:26
  • So your saying this is not possible or are you trying to tell me that I'm doing it wrong? I'm not inventing anything here, this has been used in a lot of places before, hence I'm quite confident it's possible. All I need to know is, how? Commented Apr 10, 2012 at 1:30
  • 1
    @Amber There is a plethora of cases where you may want to do what Chris is trying to achieve. What befuddles me is why you cant simply answer his question without knowing why he wants to do it that way? Nothing is more useless and time-wasting than answering a straight question with "why". Do you even know the answer? You wasted Chris's time, and now you've also wasted my time an energy - and that of many others - to read your completely useless argument. Can you please - if possible - delete your comments from here so you don't continue waste peoples time and energy for years to come? Commented Oct 14, 2013 at 4:18

4 Answers 4

4

Although I agree with @Amber's comments, you can do what you are trying to do, but you cannot use the constructor for that as you are not necessarily instantiating an object:

$arg = array( my data is here );
class Article
{
    static $title;

    static function set_static()
    {
        global $arg;
        self::$title = $arg['title'];
    }

    public static function title()
    {
        return self::$title;
    }
}
Article::set_static();

I would also pass the value as an argument, but that does not change the way it works.

Sign up to request clarification or add additional context in comments.

1 Comment

@Baba 's answer also worked, but was a little bit too bloated. Yours however is as good as it can get (I boldly assume). I will at some point do things differently, but as far as this goes it'll have to do. Thank you.
1

Use Singleton

$arg = array("title"=>" my data is here ");
class Article
{
    static $title;
    static $instance = null ; 

    public function __construct()
    {
        self::$title = $GLOBALS['arg']['title'];
    }

    public static function getInstance()
    {
        if(self::$instance === null)
        {
            self::$instance =  new self();
        }

        return self::$instance;
    }
    public static function title()
    {
        self::getInstance();
        return self::$title;
    }
}


var_dump(Article::title()); //Returns string ' my data is here ' (length=17)
var_dump(Article::getInstance()->title()); // Returns string ' my data is here ' (length=17)

Comments

0

You should initialize it in the first place.
Static $title = ...
The method --construct is called when you new an instance.

Comments

0

I use a static function, and for inefficient variable loads I have it access a private or protected member, as follows:

$arg = array('title' => 'My Title');
class Article
{
    static function title()
    {
        global $arg;
        return $arg['title'];
    }

    // or if the var load is inefficient ...
    protected static $title_md5;
    static function title_md5()
    {
        if (!isset(self::$title_md5))
            self::$title_md5 = md5(self::title());
        return self::$title_md5;
    }
}
echo(
    'title = ' . Article::title() . "\n" . 
    'title_md5 = ' . Article::title_md5() . "\n"
);

Outputs:

title = My Title
title_md5 = 5badc643b79fdda9775c45b46540afc0

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.