Menu
Well What is better Classes of Funtions?
and do you know of any good classes website tutorials? o have been on PHP.net [url]http://us3.php.net/manual/en/book.classobj.php
There is no "better", they both serve different purposes.[/QUOTE]
A class may contain its own constants, variables (called "properties"), and functions (called "methods").[/QUOTE]~
There is no "better", they both serve different purposes.[/QUOTE]
This is the best answer I've seen in this thread. You can't just decide between using classes or doing things procedurally. Once you examine you code and you find that you are able to group similar functions together, then do so by using classes. There are some circumstances where separate functions just don't group together logically into a class, although there are lots of circumstances where they do.
The most important thing to learn is not how to build classes or code procedurally, it's to recognize what code that can AND should be grouped into a class.[/QUOTE]
In OOA&D(Object Oriented Analysis and Design) class I was taught to use the crud matrix (http://www.databaseanswers.org/data_migration/crud_matrix.htm ) to help identify re-usable code and classify it. If it could be conceptually classified then it usually made sense to classify it in code.
In my mission to learn OOD I have gone over board trying to put everything in classes so it might be time to go back to using a crud matrix.[/QUOTE]
While I suspect it's not exactly what you mean, upon reading that, one might assume you are describing a bottom-up approach: determining what functions are needed, and then delegating those functions to classes as a sort of afterthought.
For truly effective object-oriented implementation, you want to start from the top: determining what objects are needed to model the functional requirements, and then deciding what properties and methods are needed for the classes that define those objects. (In reality, you can't help but think of some of the functions/methods you'll need, and that will help you determine what objects will be needed; but you still want to emphasize a top-down view, at least during the design phase, IMO.)[/QUOTE]
actually i don't see any reason to use classes. you can do ABSOLUTELY the same with plain functions.[/QUOTE]
actually i don't see any reason to use classes. you can do ABSOLUTELY the same with plain functions. if you want it to be more organized, you can group them into separate files and then include() them depending on the situation... please correct me if i'm wrong... i've googled a lot but i haven't found anything useful about that...[/QUOTE]
Also, you can do [i]absolutely[/i] the same with C as you can with PHP, so there's no reason to use PHP, right? But we do use PHP, and we do use classes, because both come with benefits.
OOP [I]emphasizes[/I] modularity, where you pass around not just raw data, but also the code that operates on that data, so that the data itself can stay encapsulated and hidden from the larger scope of the application. OOP makes it easier to reuse code with features such as inheritance. Inheritance lets us delegate some work to the compiler that we would otherwise have to do manually in a procedural language. OOP also gives us more tools for "designing-by-contract," such as interfaces.[/QUOTE]
So you can do the same without them.. if not, show me an example.[/QUOTE]
I think you misunderstood the argument everyone made. No one said you [I]can't[/I] do the same without classes, but that's not the same as saying it's [I]better[/I] without classes. Like NogDog's example: Just because you [I]can[/I] do the same without functions doesn't mean it's [I]better[/I] to do the same without functions.[/QUOTE]well, i think it's better without them... it's much easier to use functions... you don't need to create objects and so on... you just call the function.
I used to think very much the same way I would go absolutely function happy and had functions that would do everything but later after those function includes grew so large, I actually found myself forgetting about functions that I've already written.well, you can make a function for all your database actions... like database('query',$query); and the readability is **** because you put all your classes in one file... i don't really understand what you mean by saying that it's hard to read functions... if they're in different files, there's much less scrolling to do...you just open the file you need and it has about 100 lines... i think it's much better than having one file with 5000 lines... you'll spend 10 minutes scrolling and you'll probably forget what you were searching for xD. oh and use notepad++ or other tabbed text editor...
when I began to think of classes as being objects, or in specific, having a specific "task" to do, it logically groups the variables and functions together and makes their purpose more obvious by the Association within the task. Also, when you have the inclusion of many functions, it makes it difficult in troubleshooting when someone picks up your code and tries to analyze it. If an error occurs within a class, I find it easier to go to....but most importantly, I think the readability of the code is improved.
As was mentioned before, this all applies mostly to modular code...for example, I have a single database access class I use for all my database actions....most apps will access a database, so the modularity is a great asset. If I was building a small app, I probably wouldn't bother with a class, unless there was written already[/QUOTE]
Similarly, you can do the same thing without functions: just make generous use of copy-and-paste, and perhaps some judicious use of GOTO. :-)[/QUOTE]yeah and you'll have a nice 10 000 lines file... thanks, no...
well, you can make a function for all your database actions... like database('query',$query); and the readability is **** because you put all your classes in one file... [/QUOTE]
i don't really understand what you mean by saying that it's hard to read functions... [/QUOTE]I said they can get hard to follow...if you use an IDE to program, the IDE (just like the PHP parser) will automatically isolate the variables and functions within the class you are working in. Using libraries of included function files, the editor will include all functions regardless of relevance or naming convention I use on the files.
[code=php]
$qry = new dbQry($db_creds);
$qry->set_conn_type('mysql');
$qry->set_qry('SELECT * FROM contacts');
$qry->execute("s");
$contact_tbl = $qry->output_result('tbl');
////////////////////////////
// Then within the same instance of the class, I could get another table of info without having to pass the params again
////////////////////////////
$qry->set_qry('SELECT * FROM products');
$qry->execute('s');
$product_tbl = $qry->output_result('tbl');
[/code]
if they're in different files, there's much less scrolling to do...you just open the file you need and it has about 100 lines... i think it's much better than having one file with 5000 lines... you'll spend 10 minutes scrolling and you'll probably forget what you were searching for xD. oh and use notepad++ or other tabbed text editor...[/QUOTE]
0.1.9 — BETA 5.17