Php encapsulation problems

Source: Internet
Author: User
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 =" ";$ B = $ B. $;}$ B = $ B ."
". $ D ." ". $ T ['name']." ". $ T ['age']." ". $ T ['sex']."
";
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.