php - Object Oriented Programming, extending properties -
OK This is not just a PHP question, but I have this problem in PHP so that I will post an example in PHP. Example:
Example:
I have three squares, say, data_ summary, person, and student, now I have an array of data related to each object.
Class person reaches the DataAbet {Protected $ data; // name, gender, etc.} Classroom student protects {secure $ data; // student_id, etc}
Now, those "data" properties are from database tables, for example, table_person, table_student.
At the moment, we will parse the square Get_class () function to get the data from the name and their respective tables.
class data_abstract {public function __construct () {$ name = get_class ($ this); $ Table = 'table_' $ name; $ The-> Data = DB-> Query ('SELECT * FROM'. $ Table); // Whatever DB is continuing, // I'm just trying to get all the lines of data from the corresponding table. }}
OK, now the problem is, when I invest by $ student = new student (student); The constructor will find the data from table_student for me and it will be inserted into $ student-> data, but I will not be able to get data from table_person and I can put those two data in one object.
By expanding another category, we can expand all methods (work) through customized (through polymorphism), but extending properties / properties of each level object, manually Without manual construction, it seems difficult.
Is there any way in the abstract level to achieve it?
(Thanks for reading, wish I could clear the question.)
- You get $ data Want all your DB data in;
- $ data table should contain feeds with data from 'table_classname';
- Get "Data" from $ data loaded in the parent category with that data.
In that case, you should make $ data purchases external so that you can overload the data. I have edited, now we have an example of a work:
class DataAbstract // class name {public function __construct () {$ this- & gt; It is better to use caps for loadData (); } Public Function Load Data () {// $ or $$} does not use the received class, it will reference children $ table = 'table_' __counter__; $ This- & gt; Data = array ($ table); }} Class person increases the data break (public function __ composition) {patrett :: __ construct ()); } Public Function loadData () {Original: Load Data (); $ Table = 'table_' __counter__; $ This- & gt; Data = array_merge ($ this-> data, array ($ table)); }} The student of the class broadens the person {public function __ composition () {origin: __ creation (); } Public Function loadData () {Original: Load Data (); $ Table = 'table_' __counter__; $ This- & gt; Data = array_merge ($ this-> data, array ($ table)); }} $ C = new student (); Print_r ($ c> data);
output
array ([0] = & gt; Table_DataAbstract [1] => Table_person [2] => Table_ology) < / Code>
BTW, remember that PHP has got introspection, allowing you to set up fields dynamically: perhaps a cleaner using a larger array
If you enter all field names, you can do something like
function populate ($ data) // db {$ fieldList = get_class_vars ($ this) from array get receive; // The entered list is a foreign currency ($ field $ key as list => $ value) {$ this- & gt; $ Key = $ data [$ key]; // field one by one with array}}
Comments
Post a Comment