2

I try to create some sort of setup class, like global values for the page.

The PHP-code

class globals
{
    public $page;

    public function __construct()
    {
    }

    public function set_page($value)
    {
        $this->page = $value; // Maybe from a database
    }
}

class get
{
    public function page()
    {
        $globals = new globals();
        return $globals->page;
    }
}

$globals = new globals();
$globals->set_page('My value');

echo get::page(); // Short function to be in a template

Question

  • My class forget the value I set. Why is that?
  • Do I have to use global variables?
  • Is this the correct approach for the problem?
1
  • gotta set the page, to get the page. Commented Nov 2, 2012 at 12:55

7 Answers 7

3

The variable is set on an object, not on a class.

For each class, you can instantiate multiple objects. Each of those have their own variable scope.

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

Comments

2

Edit:
I forgot to include the easiest, and least verbose solution to your problem. AFAIK, you're looking for a way to check what page you're on. Constants will do just that:

defined('MY_CURRENT_PAGE') || define('MY_CURRENT_PAGE','My Value');
//use anywhere like so:
echo 'Currently on page: '.MY_CURRENT_PAGE;

My class forget the value I set. Why is that?

Quite simple: your page member function isn't static, yet you call it as though it is: get::page(). Even if you were to fix this, you're creating a new instance in the page method, but you're not preserving a reference too it anywhere, so each page call will create a new globals instance, that has nothing set.

Do I have to use global variables?

No, unless you're Really desperate, never use globals

Is this the correct approach for the problem?

No, if it doesn't work, it's not correct (IMHO).

Well, what is, you might ask. There are several ways to go about this:

class globals
{
    public static $page = null;//make this static, meaning all instances will share this var

    public function set_page($value)
    {
        self::$page = $value; // Maybe from a database
    }
}

class get
{
    private $_globalsInstance = null;
    public function __construct(globals $instance = null)
    {
        $this->_globalsInstance = $instance;
    }

    private function _getGlobals()
    {
        if (!$this->_globalsInstance instanceof globals)
        {
            $this->_globalsInstance = new globals();
        }
        return $this->_globalsInstance;
    }

    public function page()
    {
        return $this->_getGlobals()::$page;
    }
}

Personally, however, I wouldn't work like this, I'd just pass my instances to wherever I need them (as arguments to functions/methods or just instantiate them in a scope that will be accessible:

class globals
{
    public $page = null;//make this static, meaning all instances will share this var

    public function set_page($value)
    {
        $this->page = $value; // Maybe from a database
    }
}
$page = new globals();
$page->set_page('foobar');
someFunction($page);
$someObject->renderPage($page);
require_once('specificScript.php');
//inside required script:
echo $page->page;

Comments

1

Do I have to use global variables?

Not, if your can use PHP 5.3

Is this the correct approach for the problem?

Better to use a generic class for this, or use static properties of objects

<?php

class globals
{
    public static $page;

    public function __construct()
    {
    }

    public function set_page($value)
    {
        self::$page = $value; // Maybe from a database
    }
}

class get
{
    public static function page()
    {
        return globals::$page;
    }
}

$globals = new globals();
$globals->set_page('My value');

echo get::page(); // Short function to be in a template

P.S. But this is not a nice approach

2 Comments

That worked. It's important to me to keep the get::page(). What is a better approach than mine?
@JensTörnell: Why do you consider the get::page() important... if all this code does is tell you what the current page is, you could define a constant BTW: 1 line defined('CURRENT_PAGE') || define('CURRENT_PAGE','My Value');. Basically, this checks if the constant is set, if not it defines it, and sets the value to My Value. A constant is available globally and doesn't use as much resources as classes
1

$globals there

class get
{
    public function page()
    {
        $globals = new globals();
        return $globals->page;
    }
}

and there

$globals = new globals();
$globals->set_page('My value');

are different inctances of globals class.

One of the solutions is to make $page var static

public static $page;

I hope this helps

UPD:

Also you might apply Singleton to globals class and request for its insnance instead of creating new one directly:

globals::getInstance()->setPage('Page');

and

return globals::getInstance()->getPage();

In this case $page doesn't have to be static.

Comments

1

I'm not sure the other answers are very clear. You have created 2 classes. As such they have different scopes. As writen you can't access the original variable $page from the get class because it's outside the scope. Your page function in fact creates a new version of the object $globals without $page set. Normally you would place both your set and get functions in the initial object/class. Though it would be possible to use two class by calling the first class from the second and setting the page. Why you would want to do that I'm not sure.

if I were writing the class it would look like this.

class globals
{
    public $page;

    public function __construct()
    {
    }

    public function set_page($value)
    {
        $this->page = $value; // Maybe from a database
    }

    public function get_page()
    {
        return $this->page;
    }
}

Actually I would probably set page to private not public. As public I guess you don't need a get function.

Comments

0

for using methods of the class without object you must use static definition. but anyway you put value for one class object and try to get it from another...

Comments

0

Perhaps this will help you continue on your coarse:

     class globals
     {
        public static $page;

        public function set_page($value)
        {
           self::$page = $value; // Maybe from a database
        }
     }

    class get extends globals
   {
      public function page()
     {
        $globals = new globals();
        return parent::$page;
     }
   }

   $globals = new globals();
   $globals->set_page('My value');

   echo get::page();

?>

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.