简体   繁体   English

PHP - 类忘记设置变量

[英]PHP - class forget set variables

I try to create some sort of setup class, like global values for the page. 我尝试创建某种安装类,如页面的全局值。

The PHP-code PHP代码

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? 这是解决问题的正确方法吗?

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. 每个都有自己的变量范围。

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. AFAIK,您正在寻找一种方法来检查您所在的页面。 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() . 非常简单:您的page成员函数不是静态的,但您可以将其称为: 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. 即使你要解决这个问题,你也是在page方法中创建一个新实例,但是你也没有在任何地方保留引用,所以每次page调用都会创建一个没有设置的新的globals实例。

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;

Do I have to use global variables? 我必须使用全局变量吗?

Not, if your can use PHP 5.3 不,如果你可以使用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

PS But this is not a nice approach PS但这不是一个好方法

$globals there $globals那里

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. globals类的不同的。

One of the solutions is to make $page var static 其中一个解决方案是使$page var静态

public static $page;

I hope this helps 我希望这有帮助

UPD: UPD:

Also you might apply Singleton to globals class and request for its insnance instead of creating new one directly: 您也可以将Singleton应用于全局类并请求其insnance而不是直接创建新的:

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

and

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

In this case $page doesn't have to be static. 在这种情况下, $page不必是静态的。

I'm not sure the other answers are very clear. 我不确定其他答案是否清楚。 You have created 2 classes. 你创建了2个类。 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. 在写入时,您无法从get类访问原始变量$ page,因为它超出了范围。 Your page function in fact creates a new version of the object $globals without $page set. 您的页面函数实际上创建了一个没有$ page设置的对象$ globals的新版本。 Normally you would place both your set and get functions in the initial object/class. 通常,您可以将set和get函数放在初始对象/类中。 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. 作为公众我猜你不需要get函数。

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... 但无论如何你为一个类对象赋值,并试图从另一个类对象中获取它...

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();

?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM