我想从一个文件表中提取关于一个文件的所有信息,但是这个表的结构可能会改变。
因此,我想从表中提取所有字段名,并使用它们来生成包含信息的类变量,然后将选择的数据存储到它们中。
这可能吗?
是的,你可以看到php重载。
http://php.net/manual/en/language.oop5.overloading.php快速示例:(不是这不是很好的用法)
<?php
class MyClass{
var $my_vars;
function __set($key,$value){
$this->my_vars[$key] = $value;
}
function __get($key){
return $this->my_vars[$key];
}
}
$x = new MyClass();
$x->test = 10;
echo $x->test;
?>
样本
<?php
class TestClass
{
public $Property1;
public function Method1()
{
$this->Property1 = '1';
$this->Property2 = '2';
}
}
$t = new TestClass();
$t->Method1();
print( '<pre>' );
print_r( $t );
print( '</pre>' );
?>
TestClass Object
(
[Property1] => 1
[Property2] => 2
)
可以看到,一个未定义的属性是通过使用对$this
的引用赋值来创建的。所以是的,你可以在类方法中定义类变量