类与in_array
程序员文章站
2022-05-25 18:36:09
...
test.php
<?php
class test{
private $name;
private $pas = 'ddd';
private $name1 = true;
function __construct($array1=array()){
foreach ($array1 as $key=>$value){
if(!in_array($key,array_keys(get_class_vars(get_class($this))))){
continue;
}
$this->setOption($key,$value);
}
}
private function setOption($key,$value){
$this->$key = $value;
}
}
?>
index.php
<?php
require 'test.php';
$object = new test(array('name'=>'xuepeng11'));
var_dump($object);
?>
<?php
class test{
private $name;
private $pas = 'ddd';
private $name1 = true;
function __construct($array1=array()){
foreach ($array1 as $key=>$value){
if(!in_array($key,array_keys(get_class_vars(get_class($this))))){
continue;
}
$this->setOption($key,$value);
}
}
private function setOption($key,$value){
$this->$key = $value;
}
}
?>
index.php
<?php
require 'test.php';
$object = new test(array('name'=>'xuepeng11'));
var_dump($object);
?>