The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 01:45:37 AM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: oop and databases  (Read 4790 times)
walrus
Rookie
**
Offline Offline

Posts: 46


View Profile
« on: January 26, 2010, 06:12:20 AM »

Me asking the following questions is the result of never having read any good books on programming.

I'm having trouble understanding how to write oop php code for accessing a database. I have some cross reference tables and I guess that's where I'm having problems.

This isn't my real db, but I guess it's as common as they get, let's say I have the following tables:

customers
-customer id
-customer name
-other data...


product
-product id
-product name
-price
-other data...


cross reference table
-customer id
-product id



I would have a "customer" class in php, with customer->add(), customer->get_something(), a "product" class , product->add(), product->get_price(), and similar methods.



If I want to find out what a customer is buying, do I :

-add a customer->get_shopping_cart() method ?  -> this wouldn't really use just the customer table

-make a new class  .. shopping_cart->get_for_user() -> this would access methods from both the customer and the product tables, perhaps inherit them???

-add a new method in the products class?

-let oop rest and use stored procedures ? - I think this is the best way to go, but I still need to understand how to deal with the oop.


This is a simple problem, I understand how the sql part works, but from an oop point of view it's not obvious to me what the best solution is.

Book titles are also appreciated, I am feeling oop-stupid.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: January 26, 2010, 12:03:54 PM »

Wow, that's a huge set of questions.

IMO you're going about this the wrong way. First, why must you use OO as a programming methodology? Not that I'm opposed, in fact it's what I do - but I'd like to understand why for you.

Second, rather than going right at using the syntax of OO, it would be better for you to get your arms around what OO methodology actually is. WHY you create a class structure. What the benefits/downfalls of hierarchies are.

As soon as you talk $db->customer->getCart() or such you've just described a pretty huge amount of code and made the assumption of the interaction between "living" entities. OO is a challenge enough for the n00b that encapsulating DB activities within a class hierarchy is, IMO, probably beyond what you should tackle on your first bout.

And all that said, I'm afraid that in this one particular line of learning I have no book recommendations. I've always personally taught OO, have never recommended a book. J'pologize.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
isthisthingon
Global Moderator
Lifer
*****
Offline Offline

Posts: 2879



View Profile
« Reply #2 on: January 26, 2010, 02:17:13 PM »

Word: http://headfirstlabs.com/books/hfooad/

Logged

I would love to change the world, but they won't give me the source code.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #3 on: March 01, 2010, 02:40:42 PM »

Yah man sounds like you're trying to use a functional approach to solving an OO problem...

Definitely read up a little on OOP.
Logged

hai
herbacious
Journeyman
***
Offline Offline

Posts: 51


View Profile
« Reply #4 on: March 23, 2010, 01:14:39 AM »

just do what every PHP coder does - make a new MVC framework Smiley
Logged
isthisthingon
Global Moderator
Lifer
*****
Offline Offline

Posts: 2879



View Profile
« Reply #5 on: March 23, 2010, 06:14:01 AM »

just do what every PHP coder does - make a new MVC framework Smiley

Make a new MVC framework from a scripting language that doesn't even precompile down to bytecode like Ruby and Python?  Better call Facebook to get their homegrown PHP to C++ precompiler  ROFLMAO

I've seen so many performance issues in PHP lately it's mind boggling.
Logged

I would love to change the world, but they won't give me the source code.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: March 23, 2010, 10:00:38 AM »

I've seen so many performance issues in PHP lately it's mind boggling.

It's true that if you want to use PHP in a production, high end system you need to do it correctly - there's definitely right and wrong ways to do things.

Anything specifically you want to discuss?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #7 on: March 23, 2010, 02:44:00 PM »

Make a new MVC framework from a scripting language that doesn't even precompile down to bytecode like Ruby and Python?  Better call Facebook to get their homegrown PHP to C++ precompiler  ROFLMAO
If speed is an issue, you should be using Perl, which has a much faster VM (virtual machine) then python.
Better yet use a Lisp or Scheme which compiles directly into machine code. Most of the time it will can even compete directly with C++ code.

Attraction of Ruby is that it offers many features that mainstream languages do not offer, like blocks and call/cc.

Python is like glue, it holds shit together. It is probably the most flexable language.

PHP was designed to script webpages, plain and simple. It gets the job done.
What he wants is something like sqlobject http://www.sqlobject.org/ for PHP.
Ages ago (like 10 years ago) when i was messing around with PHP i made a simple version of it for PHP.
It would not be too hard to make, it would save a lot of time.

Make a class which connects to a DB.
Then have keys of the class which map to the actual database entry.
Then have the object when it is created, automatically generate sql code etc.

Start with just maybe mysql, then u could stick other backends on it.


Logged
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #8 on: March 23, 2010, 04:13:17 PM »

I think what you've come across is the object / relational DB miss match that plagues OO programmers Walrus. The code you describe is similar to the Active Record DB access model where entities are responsible for their own persistence - this gets tricky when dealing with relationships as you've identified.

There are loads of different ways of dealing with this, as hinted at by everyone, but my preferred solution is to not reinvent the wheel and to use an ORM (object relational mapper) library such as Doctrine - version 2.0 currently in development is really comprehensive and quite unobtrusive.

In terms of good books, I like PHP Objects Patterns and Practice by Matt Zandstra and the classic Patterns of Enterprise Application Architecture by... erm someone else. The latter isn't a PHP book but it is really strong on common OOP problems and how to use good architecture to solve them.

DM
Logged
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #9 on: March 23, 2010, 05:39:10 PM »

my new angle of attack to get the fastest code possible, is to not code anymore. LOL
Perk has been very effective at removing coding from my plate lately.  ROFLMAO
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #10 on: March 23, 2010, 05:52:48 PM »

There is no spoon.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
isthisthingon
Global Moderator
Lifer
*****
Offline Offline

Posts: 2879



View Profile
« Reply #11 on: March 23, 2010, 07:48:38 PM »

>Anything specifically you want to discuss?

Discovering that expression evaluation with operatios actually treats the operators like function calls.  X = X + 1 forces a function call to resolve the +, under the hood.  This has caused me great strife with code performance with nested loop structures.  As a solution I've been told to consider array_walk when checking, for example, the value of each array element that in turn calls a function.  So instead of a while loop, or a for each, simply iterate through an array calling it's "resolve" function with array_walk.

Additionally, APIs written in C/C++ should be used - especially for bulk operations - in favor of working things old skool style (with your code fingers).
Logged

I would love to change the world, but they won't give me the source code.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #12 on: March 24, 2010, 09:40:14 AM »

the array iteration routines are the bomb, speed-wise. Also, I believe that $i += 1 will compile down differently than $i = $i + 1. I've not done any speed testing, but I believe I remember seeing that somewhere.

@ C++ libs: they ROCK. I wrote a little routine in vanilla C and attached it to PHP for doing a simple string routine - walking an array to that routine just BLAZED compared to similar code written in pure PHP. With a bit of a "critical path" notion, you can do most things quite comfortably in PHP and then if you need to really rock, put just those pieces in C.

I also am getting a lot more speed out of using mysqli with multi-line SQL instructions, as well (of course) with stored procedures. Yes, I know that scalability argument with stored procedures but for the most part I don't need to scale to Google size tomorrow. Distributing the work makes a profound difference, provided you're comfy with having logic in a couple different places.

But that said, my framework currently uses MySQL stored procedures and functions, PHP, C under PHP for special stuff, JavaScript for client side logic and CSS for all presentation qualities of a page. So what's another language thrown into the mix to make things go faster?  ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #13 on: March 24, 2010, 03:12:24 PM »

But that said, my framework currently uses MySQL stored procedures and functions, PHP, C under PHP for special stuff, JavaScript for client side logic and CSS for all presentation qualities of a page. So what's another language thrown into the mix to make things go faster?  ROFLMAO
PHP to the best of my knowledge does not have cython or pyrex.
http://www.cython.org/ Basically a python to c compiler for extensions.

Or psyco http://psyco.sourceforge.net/
A JIT compiler that compiles down to machine language.
It is a lot easier to make extensions with python and cython then it is with PHP.
Fastest language of all is perl. But making extensions with it is a bitch.

Again it depends what u are doing with the language.
Making an extension that uses a C/C++ library for a desktop application is a different story compared to making one that is being used on a webserver.

You are better to get speed increase using techniques like ....
Quote
I also am getting a lot more speed out of using mysqli with multi-line SQL instructions
Everytime u do 1 sql instruction at a time, the result have to be converted from C to the scripting language.

A misfunctioning extension can be prone to memory leaks, or memory corruption because of garbage collection done by scripting language. So u could have a buffer overflow caused by an extension.

To be a fuking smart ass, fastest language is ocaml.
http://en.wikipedia.org/wiki/Objective_Caml
It probably knocks the socks off any other language speed wise, and offers garbage collection.

Xavier Leroy has cautiously stated that "OCaml delivers at least 50% of the performance of a decent C compiler"[1], and benchmarks have shown that this is generally the case[2]. Some functions in the OCaml standard library are implemented with faster algorithms than equivalent functions in the standard libraries of other languages. For example, the implementation of set union in the OCaml standard library is asymptotically faster than the equivalent function in the standard libraries of imperative languages (e.g. C++, Java) because the OCaml implementation exploits the immutability of sets in order to reuse parts of input sets in the output (persistence).

Ocaml algorithms generally speaking rely heavily on recursion. In some cases algos implemented in Ocaml are even faster then C. Also Ocaml has bignums. So if u tell it to compute the factorial of 200, it gives u the exact number.

Ocaml is used by mainly academia. So if u want to compute shit when u are designing a particle accelerator, probably would be a good language Smiley.

Use the right language for the job.
Logged
isthisthingon
Global Moderator
Lifer
*****
Offline Offline

Posts: 2879



View Profile
« Reply #14 on: March 25, 2010, 08:26:23 PM »

I recommend this.
Logged

I would love to change the world, but they won't give me the source code.
Pages: [1] 2
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!