Php encapsulation problem [codePHP] [code] defines a User class classUser {private $ arr; for encapsulation, do not use function _ construct () {$ this-& gt; arr $ this-& gt; get_data (); constructor, which will be executed when the class is instantiated} publicfunctioni php encapsulation problem
[Code = PHP] [/code]
Defines a User class
Class User {
Private $ arr; // encapsulate it to prevent external use.
Function _ construct (){
$ This-> arr = $ this-> get_data (); // Constructor, which is executed when Class 1 is instantiated.
}
Public function isNotExist (){
Echo "Does not exist
";
Echo "re-input ";
}
Private function get_data (){
$ Arr = array (
Array ("name" => "Michael", "age" => 12, "sex" => "male "),
Array ("name" => "Li Si", "age" => 25, "sex" => "male "),
Array ("name" => "Wang Wu", "age" => 18, "sex" => "male "),
Array ("name" => "Lili", "age" => 16, "sex" => "female "),
Array ("name" => "Chen Ming", "age" => 25, "sex" => "male "),
Array ("name" => "Zhang Xiao", "age" => 25, "sex" => "female "),
Array ("name" => "Li Ming", "age" => 15, "sex" => "male "),
Array ("name" => "Zhao Liang", "age" => 25, "sex" => "male "),
Array ("name" => "Zhang Yue", "age" => 22, "sex" => "male "),
Array ("name" => "Wang Qing", "age" => 21, "sex" => "female ")
);
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. $;}$ B = $ B ."
";
Return $ B;
}
// The following is the inherited User class, which cannot be changed.
Class newUser extends User {
Function get_both_sex_ages ($ sex, $ age1, $ age2 ){
}
I need to call $ arr in the method above. I don't know what to do.
------ Solution --------------------
The base class Private derived class cannot be accessed. this is a general object-oriented rule. either the base class directly provides the public/protected interface for accessing the private attribute for the derived class, you can either set the attribute to protected so that the interface of the derived class can directly access it.