简体   繁体   English

PHP中的类静态函数变量参考

[英]Class static function variable reference in PHP

PHP 7. PHP 7。

I'm trying to use the same class method in different situations. 我试图在不同情况下使用相同的类方法。 The first time, when the user is creating a new entry into the database, and I draw the admin page with empty values. 第一次,当用户在数据库中创建新条目时,我用空值绘制了管理页面。 It has been created as the following. 它的创建如下。

$classname::drawAdmin(FALSE);

$classname is a variable, since I don't know in advance which classes will be used on the webpage (Ikr?), and have to get it from the database. $ classname是一个变量,因为我事先不知道网页上将使用哪些类(Ikr?),并且必须从数据库中获取它。

Inside the method, it checks if it's creating a new object, or reusing an existing one. 在方法内部,它检查是否正在创建一个新对象,或重用现有对象。

static function drawAdmin( $mod = FALSE ) {
        print 'Technology<input type="text" name="technology" value="'.($mod?$this->technology:"").'" /><br />';
etc...
}

It works well when a new entry is created, check if it's trying to modify, if not, create an input with no value, else it is writing the objects datas into the values. 创建新条目时,它工作良好,请检查它是否正在尝试修改,如果没有,则创建一个没有值的输入,否则它将对象数据写入值中。

However it does not work when I create a new object, and try to modify it's data. 但是,当我创建一个新对象并尝试修改它的数据时,它不起作用。

$class = new $row['class_name']($_GET['id']);
$class->drawAdmin(TRUE);

(The class' constructor sanitizes the $_GET array.) (类的构造函数清除$ _GET数组。)

In this case, I receive an error message: 在这种情况下,我会收到一条错误消息:

Fatal error: Using $this when not in object context in [censored]/class.phone.php on line 932

even if the object was created correctly, and existing (checked with var_dump). 即使对象已正确创建并存在(使用var_dump检查)。

Haha, jokes on me, it's a static method, so I can't use $this! 哈哈,对我开玩笑,这是一个静态方法,所以我不能使用$ this! However, when I change the print to: 但是,当我将打印内容更改为:

static function drawAdmin( $mod = FALSE ) {
        print 'Technology<input type="text" name="technology" value="'.($mod?self::technology:"").'" /><br />';
etc...
}

I still get the error message: 我仍然收到错误消息:

Fatal error: Undefined class constant 'technology' in [censored]/class.phone.php on line 932

and if I add the (protected) variables to static, I can't use them anymore with $this->. 如果我将(受保护的)变量添加到静态变量,则无法再通过$ this->使用它们。

Is there any way, that I could use the same class variables with both object and without object contexts? 有什么办法可以在对象和对象上下文之间使用相同的类变量?

I think you could keep the method static, but if you're editing an existing object, you'd have to pass an actual instance of the same class into the method as optional second parameter, to be used when $mod is true, and then use the "technology" value from that instance. 我认为您可以使该方法保持静态,但如果要编辑现有对象,则必须将同一类的实际实例作为可选的第二个参数传递给该方法,以在$ mod为true时使用,并且然后使用该实例中的“技术”值。

For example: 例如:

static fuction drawAdmin($mod=FALSE, $class=NULL) { 
        print 'Technology<input type="text" name="technology" value="'.($mod?$class->getTechnology():"").'" /><br />';
}

and then in the case of it being a modification, call it like this: 然后,如果是修改的话,可以这样称呼它:

$class->drawAdmin(TRUE, $class);

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

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