简体   繁体   English

PHP类:通过构造函数分配静态属性

[英]PHP class: assigning static property via constructor

Simplified example of a class: 类的简化示例:

class Table extends TableAbstract{
    protected static $tablename;

    function __construct($str){
       $this->tablename = "table_" . $str;
       $this->insert();    // abstract function
    }

}

When I've used classes like this in the past I've assigned the $tablename directly when writing the class. 当我过去使用这样的类时,我在编写类时直接分配了$ tablename。 This time however I would like it to be decided by the constructor. 但是这次我希望它由构造函数决定。 But when I then call the function referencing $tablename the variable seems to be empty, when I echo the SQL. 但是当我调用函数引用$ tablename时,变量似乎是空的,当我回显SQL时。

What am I doing wrong, or could someone suggest a way to achieve what I want here? 我做错了什么,或者有人建议一种方法来实现我想要的东西?

Thanks for any comments/answers.. 感谢您的任何意见/答案..

由于属性是static ,使用Table::$tablename - 或者self::$tablename来访问它以隐式引用当前类。

when accessing a static property you need to use self::$varName instead of $this->varName . 访问静态属性时,您需要使用self::$varName而不是$this->varName Same thing with static methods. 静态方法也是如此。

Edit: Just to highlight some differences between abstract and static/non-static properties, I made a small example. 编辑:为了突出抽象和静态/非静态属性之间的一些差异,我做了一个小例子。

<?php
abstract class A{
    public abstract function setValue($someValue);

    public function test(){
        echo '<pre>';
        var_dump($this->childProperty);
        var_dump(B::$childStatic);
        echo '</pre>';
    }
}

class B extends A{
    protected $childProperty = 'property';
    protected static $childStatic = 'static';

    public function setValue($someValue){
        $this->childProperty = $someValue;
        self::$childStatic = $someValue;
    }
}

//new instance of B
$X = new B();
//another new instance of B
$Y = new B();

//output the values
$X->test();
$Y->test();

//change the static and standard property in $X
$X->setValue("some new value");

//output the values again.
$X->test();
$Y->test();
?>

Output: 输出:

string(8) "property"
string(6) "static"

string(8) "property"
string(6) "static"

string(14) "some new value"
string(14) "some new value"

string(8) "property"
string(14) "some new value"

After you call setValue on $X, it you can see that the values of the static property change in both the instances while the non-static property changes only in that one instance. 在$ X上调用setValue之后,您可以看到静态属性的值在两个实例中都发生了变化,而非静态属性仅在该实例中发生了变化。

Also, I just learned something. 另外,我刚刚学到了一些东西。 In a method of an abstract class trying to access a static child property, you have to specify the child class name to access the property, self:: doesn't work and throws an error. 在尝试访问静态子属性的抽象类的方法中,您必须指定子类名来访问该属性, self::不起作用并抛出错误。

If you would google up such thing as php static you would have found that: 如果你想谷歌这样的东西像php静态你会发现:

From PHP Manual : 来自PHP手册

Static Keyword 静态关键字

Static properties cannot be accessed through the object using the arrow operator ->. 使用箭头运算符 - >无法通过对象访问静态属性。

static properties are set on the class, not on an instance. static属性是在类上设置的,而不是在实例上设置的。 Get rid of the static to make your $tablename a normal instance property and it should work as expected. 摆脱static以使$tablename成为普通的实例属性,它应该按预期工作。

a static member is not tied up to the instance but it's more related to the class, so you can't really reference a static member through $this . 静态成员不与实例绑定,但它与类更相关,因此您无法通过$this真正引用静态成员。 you should use self::$staticMemberName to access a static member from within a class instance. 您应该使用self::$staticMemberName从类实例中访问静态成员。

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

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