Questions about PHP class inheritance

Source: Internet
Author: User
Concerning the inheritance of PHP classes, I have now compiled a class with two inheritance relationships. after the parent class is instantiated, the subclass does not know how to call the variables stored after the parent class has been attached to the instance.
First, paste the code below:
Class Identification {// The Identification function associates name with code. public function Identification () {// connects to InfoModel $ DB_Info = M ('info '); $ condition-> Name = $ this-> name; return $ this-> code = $ DB_Info-> where ($ condition)-> getfield ('code ');} public $ name; protected $ 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_leukocyte = $ DB_BloodTest-> where ($ condition)-> getfield ('blocks'); 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_gv', $ neal_BT-> info_bt_asset group); $ 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 shown in the code above: I instantiate the Identification class as neal and store a variable $ code, and then instantiate its subclass PT_Info as neal_BT, in this case, you need to call the $ code variable value that has been stored in neal (for details, refer to the function usage in the PT_Info class)
I don't want to store another $ code variable in the PT_Info class. I want to borrow an inheritance relationship. after all, there will be many similar structures in the future, so the write will be messy as a whole.
In other words, I first need to call the variables in an instantiated parent class in the instantiated Subclass. These two classes have an inheritance relationship in definition, however, 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 writing method.
Beg for help !!!


Reply to discussion (solution)

Your concept is wrong.
After Class PT_Info extends Identification
PT_Info and Identification are two independent classes. although PT_Info inherits Identification
This makes sense that you are not your father.

Class Identification
{
Public function Identification ()
You have defined the Identification method in Identification. this is the constructor (it is said that php7 has canceled the C ++ feature)
Then, when you instantiate PT_Info, this Identification method will be automatically executed and the code attribute will already have a value.
Note: The constructor does not return values (it is useless if it is written)

Therefore, $ neal = new Identification (); in the tclass method is meaningless.
$ Neal_BT = new PT_Info ();
That is to say, $ DB_Info-> where ($ condition)-> getfield ('code'); is executed twice, and useless once

Your concept is wrong.
After Class PT_Info extends Identification
PT_Info and Identification are two independent classes. although PT_Info inherits Identification
This makes sense that you are not your father.

Class Identification
{
Public function Identification ()
You have defined the Identification method in Identification. this is the constructor (it is said that php7 has canceled the C ++ feature)
Then, when you instantiate PT_Info, this Identification method will be automatically executed and the code attribute will already have a value.
Note: The constructor does not return values (it is useless if it is written)

Therefore, $ neal = new Identification (); in the tclass method is meaningless.
$ Neal_BT = new PT_Info ();
That is to say, $ DB_Info-> where ($ condition)-> getfield ('code'); is executed twice, and useless once


Thank you !! It is indeed an issue I have understood. it is too timely. thank you !!!

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.