php - Class with a db query, extending, functions and views. Am I doing it right way? -


There is only slight progress in my practice Most of my code works but I'm not sure I work correctly ?

Please tell me if I make a mistake and can correct me.

First, I create autoload functions:

function autoload_models ($ Models) {if (file_exists (MODELS_PATH. $ Model. .php ')) {require_once MODELS_PATH $ model' .php '; Back true; } Other {return false; }} Spl_autoload_register ('autoload_models'); Function autoload_controllers ($ controller) {if (file_exists (CONTROLLERS_PATH. $ Controller. '.php')) {require_once CONTROLLERS_PATH $ controller '.php'; Back true; } Other {return false; }} Spl_autoload_register ('autoload_controllers');

I have a class like this:

  class category {public $ db; Public $ rows; Public $ id; Public function build_category () {global $ db; Global $ rows; $ Db = new database (); $ Db- & gt; Queries ("SELECT *" from categories); $ Rows = $ db-> gt; result (); } Public function category_items () {global $ db; Global $ rows; Global $ ID; $ Db = new database (); $ Db- & gt; Queries ("SELECT * from posts WHERE category_id = '$ id'"); $ Rows = $ db-> gt; result (); }}  

I expand with other classes (There are still some problems. nothing print) < / Em>:

  category category_its category {public $ db; Public $ rows; Public $ id; Public function display_category_items () {// parent class function make the original call: category_items (); Global $ rows; Global $ ID; // Check if the page parameter is integer if (ctype_digit ($ _GET ['id'])) {$ id = $ _GET ['id']; } Other {print "invalid category page parameters"; } Foreign ($ lines as $ rows) {print "test"; Print queue $ ['post_title']; // It does not work yet    

Class to create a menu with categories (Everything works here ) :

  Category Categories_Menu Category {public $ db; Public $ rows; Public function build_category_menu () {parent :: build_category (); Global $ rows; Foreign exchange ($ line $ as line) {VIEWS_PATH is required 'categories / categories_menu.php'; }}}  

and finally the example:

  $ category_menu = new categories_menu (); $ Category_menu- & gt; Build_category_menu (); $ Category_items = new square_item (); $ Category_items- & gt; Display_category_items ();  

Thanks for your time and help!

Where to

Anyway, you should get rid of them.

I think there is no change in your lines, after any conversation. Using globals will also not be relevant in extended classes.

Your public properties and vowels are mentioned, do not interact with each other. Thus, members of the object seem completely useless.

What will I suggest in simple plans

  class model {protected $ _db; Public function __ composition (database $ db) {$ this- & gt; _db = $ db; }} Class category model {public $ _rows; Public $ _id; Public function build_category () {$ this- & gt; _db-> Query (Select "From Categories"); $ The-> _rows = $ this-> _db-> Resultset ();} Public Function category_items () {$ this- & gt; _db- & Gt; query ("SELECT * posts by WHERE category_id = '{$ this-> _id}'"); $ this-> _rows = $ this-> _db-> resultset (); / / Here you will overwrite $ _rows?} Category Categories_Amain category extension {public $ _rows; public function build_category_menu () {$ this- & gt; build_category (); Foreign currency ($-> $-> _rows $ line form) ) {VIEWS_PATH is required 'categories / categories_menu.php';}}} Category Category_Items category { Public $ _rows; public $ _id; public function display_category_items () {if (ctype_digit ($ _gET ['id'])) // // just use the interval, or is_int? $ This-> _id = $ _GET [ 'Id'];} Other {print "invalid category page parameter";} // You specify a value for $ _id, then call that function for $$ -> category_items (); Foreign currency ($ This-> _Rows $ row) {print "test"; Print queue $ ['post_title']; }}}  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -