php封装的问题
[code=PHP][/code]
定义了一个User类
class User{
private $arr;//进行封装不让外部使用
function __construct(){
$this->arr = $this->get_data();//构造函数,在类一实例化的时候就会执行
}
public function isNotExist(){
echo "Does not exist
";
echo "重新输入";
}
private function get_data(){
$arr=array(
array("name"=>"张三","age"=>12,"sex"=>"男"),
array("name"=>"李四","age"=>25,"sex"=>"男"),
array("name"=>"王五","age"=>18,"sex"=>"男"),
array("name"=>"李利","age"=>16,"sex"=>"女"),
array("name"=>"陈明","age"=>25,"sex"=>"男"),
array("name"=>"张晓","age"=>25,"sex"=>"女"),
array("name"=>"李明","age"=>15,"sex"=>"男"),
array("name"=>"赵亮","age"=>25,"sex"=>"男"),
array("name"=>"张月","age"=>22,"sex"=>"男"),
array("name"=>"王青","age"=>21,"sex"=>"女")
);
return $arr;
}
public function joinHtml($array){
if(count($array)==0){
User::isNotExist();
}
$d = 0;
$b ="
";
foreach($array as $t){
$d = $d+1;
$a = "
".$d." |
".$t['name']." |
".$t['age']." |
".$t['sex']." |
";
$b = $b.$a;
}
$b = $b . "
";
return $b;
}
//下面是继承User这个类,上面的不允许改变。
class newUser extends User{
function get_both_sex_ages($sex, $age1,$age2){
}
我需要在上面那个方法中调用$arr,不知道该怎么办,求解
------解决方案--------------------
基类Private派生类无法访问,这是面向对象的通用规则,要么基类直接提供访问private属性的public/protected接口给派生类使用,要么将属性直接设置为protected以便派生类的接口直接访问。