php - Returning database row as an object - is this a good way to do it? -


I am creating a web application in PHP Generally, the objects will be related to a specific line in the database. I want to write some classes to allow those classes to be manipulated. How it's in the form of an example:

Edit: I think it tells me what I'm trying to do, just looking for a practical example in PHP

< Pre> // Create a new company record in the database $ data = array ('name' = & gt; 'my test company';); Try {$ CompanyId = Company_Common :: create ($ data); } Hold (exception $ E) {exit ('error:'. $ E- & gt; getMessage (). "\ N"); } // Load records from database {$ company = try new company ($ Company ID); } Hold (exception $ E) {exit ('error:'. $ E- & gt; getMessage (). "\ N"); } // Change company name $ company- & gt; Name = 'company name new'; $ Company & gt; Save (); Class company_common {create function ($ data) {// here to check the code required, enter the company's record in the database, then return the line ID}} class company {public function __ composition ($ id) {// load here Add the code to the database table based on the ID table, if the database row exception is not found in the database table (mysql_num_rows ($ result) & lt; 1) {// something similar to the database based on the new exception ( 'A Company not found '); } // dynamically set $ line from database row = mysql_fetch_assoc ($ result) properties; Forex currency ($ line as $ line = & gt; $ value) {$ this- & gt; {$ Key} = $ value; $ This- & gt; Basic-> {$ Key} = $ value; }} Function save () {// original copy of database $ original = new company ($ -> -> ID); // Add code / compare code to compare this $ with original code, update query, and update any field}} Delete () {// Add code here to delete company records from database In addition, the above category will include functions such as GetContactIds, which will retrieve a bunch of line ID from the contact table based on the company ID.

I am really after some reaction to what others feel about this approach, and what to do and what can be done thanks to different / more efficiently.

I prefer to create a clear function call to draw data from the database rather than inserting it individually In the producer this allows me to create a dummy example of class for testing and joke purposes. A normal class can look like this:

  class car {// Optionally preventing cars from triggering private function in any other way __construct () {} / db public static function search Get the car from ($ ID) {// Your DB stuff here $ data = $ db- & gt; Where ('id', $ id) - & gt; get receive (); Return Self: Creating ($ data); } // Fake car - Make an example and return data with data to create a static function (array $ data) {$ instance = new self (); Return $ example- & gt; Fill ($ data); } // Set the supplied model to fill the model personal function (array $ data) {$ this- & gt; Make = isset ($ data ['create'])? $ Data ['make']: wrong; $ This- & gt; Model = isset ($ data ['model'])? $ Data ['model']: wrong; $ This return; } Save Public Function () {// save to db}}  

and will be used: car :: with data from $ car Is (1); // Data with car input $ make = $ _GET ['create']; $ Model = $ _GET ['model']; $ Car = Car :: Creating (array ('create' = & gt; $ make, 'model' => gt; $ models)); $ Car- & gt; Save (); // Car = car :: with no data $ car (array ()); The main advantage here is that you can create a mock object for unit tests and you can see the model with data from sources other than DB

. Can create. >

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -