Questions about the inheritance of PHP classes

Source: Internet
Author: User
I have now written myself a class of two inheritance relationships, after the parent class is instantiated, the subclass does not know how to invoke the variables stored after the parent class has been instantiated.
First stick the code in the following:
The Class identification{//identification function connects name to code public function identification () {//To Infomodel$db_info = M (' Info '), $condition->name = $this->name;return $this->code = $DB _info->where ($condition)->getfield (' Code ');} Public $name;p rotected $code;} Class Pt_info extends Identification{public function get_bloodtest () {$DB _bloodtest = M (' bloodtest '); $condition Code = $this->code;echo "This is code:" $this->code; $this->INFO_BT_WBC = $DB _bloodtest->where ($condition) ->getfield (' WBC '); echo "This is WBC:". $this->info_bt_wbc; $this->info_bt_neper = $DB _bloodtest->where ($ Condition)->getfield (' Neper '); echo "This is ne%:". $this->info_bt_neper; $this->info_bt_lyper = $DB _ Bloodtest->where ($condition)->getfield (' ly% '); echo "This is ly%:". $this->info_bt_lyper; $this->INFO_BT _moper = $DB _bloodtest->where ($condition)->getfield (' mo% '); echo "This is mo%:". $this->info_bt_moper;} Public $Info _bt_wbc;public $Info _bt_neper;public $Info _bt_lYper;public $Info _bt_moper;} Public Function Tclass () {$neal = new identification (); $neal->name = ' neal '; $code = $neal->identification (); $this- >assign (' name ', $neal->name), $this->assign (' Code ', $code); $neal _bt = new Pt_info (); $neal _bt->get_ Bloodtest (); $this->assign (' INFO_BT_WBC ', $neal _BT->INFO_BT_WBC); $this->assign (' Info_bt_neper ', $neal _ Bt->info_bt_neper); $this->assign (' Info_bt_lyper ', $neal _bt->info_bt_lyper); $this->assign (' Info_BT_ Moper ', $neal _bt->info_bt_moper); $this->display ();}

As in the code above: I have stored a variable $code after instantiating the identification class into Neal, and then instantiating its subclass pt_info to NEAL_BT, you need to call the value of the $code variable that has been stored in Neal (see Pt_ function usage in the Info Class)
I do not want to save a $code variable in the Pt_info class, want to borrow the inheritance relationship, after all, there are a lot of similar structure, so the whole will be very messy
In other words, I'm going to start by instantiating a subclass to invoke a variable in the instantiated parent class, and the two classes have an inheritance relationship in the definition, but after instantiation I don't know how to tell the compiler that there is an inheritance relationship between these two classes (Neal and NEAL_BT)
Note: My entire project is written using the thinkphp framework, so some functions (such as the ' M ' method) are not the default PHP notation
Kneel for God help answer!!!


Reply to discussion (solution)

There's something wrong with your concept.
Class Pt_info extends identification after
Pt_info and identification are two separate classes, though pt_info inherit identification.
It's a reason why you're not your father.

Class identification
{
Public Function identification ()
You define the identification method in identification, which is the constructor (it is said that PHP7 has canceled the C + + feature)
Then, when you instantiate the Pt_info, the identification method is automatically executed and the Code property is already valued.
Note: Constructors do not return a value (writing is useless)

Therefore $neal in the Tclass method = new identification (); It doesn't make sense.
There are also $neal _bt = new Pt_info ();
That means $DB _info->where ($condition)->getfield (' Code '); Was executed two times, one more useless

There's something wrong with your concept.
Class Pt_info extends identification after
Pt_info and identification are two separate classes, though pt_info inherit identification.
It's a reason why you're not your father.

Class identification
{
Public Function identification ()
You define the identification method in identification, which is the constructor (it is said that PHP7 has canceled the C + + feature)
Then, when you instantiate the Pt_info, the identification method is automatically executed and the Code property is already valued.
Note: Constructors do not return a value (writing is useless)

Therefore $neal in the Tclass method = new identification (); It doesn't make sense.
There are also $neal _bt = new Pt_info ();
That means $DB _info->where ($condition)->getfield (' Code '); Was executed two times, one more useless


Thank you so much!! It is really my own understanding of the problem, you this explanation is too timely reply!!!
  • 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.