www.webdeveloper.com

View Poll Results: What's better Classes of Functions?

Voters
10. You may not vote on this poll
  • Classes

    7 70.00%
  • Functions

    3 30.00%
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: php class vs function

  1. #16
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    Similarly, you can do the same thing without functions: just make generous use of copy-and-paste, and perhaps some judicious use of GOTO. :-)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  2. #17
    Join Date
    May 2003
    Posts
    599
    Quote Originally Posted by paulens12 View Post
    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...
    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.

    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
    We do precision guesswork

  3. #18
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by Jeff Mott View Post
    Also, you can do absolutely 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 emphasizes 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.
    so that the data itself can stay encapsulated and hidden from the larger scope of the application.
    Yes but you can just put it in a file and not include it if you don't need it...
    Look, vBulletin is a really huge and powerful script.... I had been browsing its code, but couldn't find any classes... I have never seen classes in real work... I have only seen some theorical examples... So you can do the same without them.. if not, show me an example.

    Also, you can do absolutely the same with C as you can with PHP, so there's no reason to use PHP, right?
    NO, we CAN'T do the same with C. You need to COMPILE it. And it's not designed for web development. How do you use get and post data with C? And even how the hell are you gonna send html code to browser? You'll need to create a file each time... No way. A web server using C would need at least 8GB RAM for one site xD.

  4. #19
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by paulens12 View Post
    So you can do the same without them.. if not, show me an example.
    I think you misunderstood the argument everyone made. No one said you can't do the same without classes, but that's not the same as saying it's better without classes. Like NogDog's example: Just because you can do the same without functions doesn't mean it's better to do the same without functions.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  5. #20
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by Jeff Mott View Post
    I think you misunderstood the argument everyone made. No one said you can't do the same without classes, but that's not the same as saying it's better without classes. Like NogDog's example: Just because you can do the same without functions doesn't mean it's better to do the same without functions.
    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.
    iwewe

  6. #21
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by hastx View Post
    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.

    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
    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...

  7. #22
    Join Date
    Oct 2012
    Posts
    6
    Quote Originally Posted by NogDog View Post
    Similarly, you can do the same thing without functions: just make generous use of copy-and-paste, and perhaps some judicious use of GOTO. :-)
    yeah and you'll have a nice 10 000 lines file... thanks, no...

  8. #23
    Join Date
    May 2003
    Posts
    599
    Quote Originally Posted by paulens12 View Post
    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...
    ...and there is only 2 ways you are getting variables into those functions: including them as globals, or passing them into the function, like database($credentials, $type, $qry). And everytime you need to pass a query you repeat that...if you include them as globals, you have to jump out of the function and refer back to where ever you included the variable at

    Quote Originally Posted by paulens12 View Post
    i don't really understand what you mean by saying that it's hard to read functions...
    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.

    Take, for example, the task is to query a database and display the results in an HTML table:

    PHP Code:
    $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'); 
    This is a very readable way to aquire 2 (or more) full HTML tables of database results. IF there is a problem in the class, the IDE will take me there and only show relevant functions and variables...furthermore the modularity has 2 benefits:
    • I can drop that class into any app I work on
    • I can give that class to someone else to contribute to, modify or enhance, without concern of missing other dependant files or conflicting functions.



    Quote Originally Posted by paulens12 View Post
    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...
    Again, this assumes you program in a text editor. An IDE like PHPStorm, or NetBeans...or even Geany (if you wanna be free and mimalist) will really help you not have to focus so much on your own filing system.

    I think the biggest things to consider in functions vs classes is 1) The size, function and speed at which you can create app...and 2) Whether you are in a collaborative environment. IF you are the only one working on the code for personal use, use any system you like...but if you are in a collaborative environment, there is no way you will get agreement on a filing system for includes, and the program's "tasks" are usually divided among developers...But the great thing about PHP is that it allows that flexibility to decide what the best approach will be in each situation.
    Last edited by hastx; 12-28-2012 at 08:52 AM.
    We do precision guesswork

  9. #24
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,304
    Quote Originally Posted by paulens12 View Post
    yeah and you'll have a nice 10 000 lines file... thanks, no...
    Sorry: apparently I should have wrapped my reply in [sarcasm]...[/sarcasm] tags.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles