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 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: php class vs function

  1. #1
    Join Date
    Feb 2006
    Location
    Ft Lauderdale
    Posts
    299

    php class vs function

    Well What is better Classes of Funtions?

    and do you know of any good classes website tutorials? o have been on PHP.net http://us3.php.net/manual/en/book.classobj.php but nothing really usefull

  2. #2
    Join Date
    Jul 2007
    Location
    Québec
    Posts
    61
    In my opinion, it depends on the problem context.

    Sometimes a more standard, procedural approach with functions is better. However, there are cases where an OOP approach would be better since it make possible to solve the problem with less code and efforts.

    For the tutorial, I don't know a good one. But, you should have a look at http://ca.php.net/manual/en/language.oop5.php.

    A good way to learn it, is to first understand the php class syntax basics and after put your hand in an available php OO project and figure out the logic behind.

    MGB

  3. #3
    Join Date
    Nov 2008
    Posts
    2,477
    There is no "better", they both serve different purposes.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,239
    In general I agree that neither is necessarily "better", but I strongly recommend that you lean OOAD/OOP in order to improve your overall programming skills and toolkit.

    Getting started with objects with PHP V5
    Advanced PHP V5 objects

    And consider getting your hands on PHP Objects, Patterns, and Practice by the same author (Matt Zandstra).
    "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

  5. #5
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    Quote Originally Posted by Mindzai View Post
    There is no "better", they both serve different purposes.
    This is 100% correct.
    A class may contain its own constants, variables (called "properties"), and functions (called "methods").
    ~ php.net

    In this case we have apples, oranges and bananas - and that really is what we want - PHP Fruit Salad, so we can get the job done as efficiently as possible!
    ...but stupidity is terminal.

  6. #6
    Join Date
    Sep 2011
    Posts
    1

    class is bigger than function

    hello,
    you're wondering which one is better
    it's complicated because you can't tell which one is better but you can clearly see class is bigger than function.
    function is a part of class
    class can contain function but function can't hold a class.
    so in my opinion class would be better for more functions codes
    thank you

  7. #7
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    I suspect that the distinction the OP means to make is procedural vs OO.
    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";

  8. #8
    Join Date
    May 2010
    Posts
    77
    I'd have to say conceptually classes are better in all situations because they categorize and delegate functionality. But then again in some cases it might be a lot easier and faster to write a couple of simple functions rather than have a separate class. In cases like that it's not like you don't have to write the function inside the class so it actually ends up saving you some coding to just put the functions in the script where they are needed. Still, grouping everything into classes makes the logic much more logical to me.

    As far as the link you posted not having much useful, I think it has plenty of useful things if your an expert programmer but not for me. php.net is more documentation than learning material. Once I get into more intermediate and advanced topics nothing there makes any sense to me no matter how many times I read up on the concepts. The site is seriously lacking in good instruction.

  9. #9
    Join Date
    Dec 2005
    Posts
    2,984
    Quote Originally Posted by Mindzai View Post
    There is no "better", they both serve different purposes.
    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.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,239
    Quote Originally Posted by aj_nsc View Post
    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.
    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.)
    "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

  11. #11
    Join Date
    May 2010
    Posts
    77
    In OOA&D(Object Oriented Analysis and Design) class I was taught to use the crud matrix (http://www.databaseanswers.org/data_...rud_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.

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,239
    Quote Originally Posted by iansane6 View Post
    In OOA&D(Object Oriented Analysis and Design) class I was taught to use the crud matrix (http://www.databaseanswers.org/data_...rud_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.
    You could pretend you are using an OO-only language (e.g. Java) where everything has to go into a class (although people often "cheat" and create classes that are merely containers for procedural code). Of course, with PHP, you always have to have at least a few lines of code that are not part of a class in order to load and instantiate the "main" class.
    "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

  13. #13
    Join Date
    Dec 2005
    Posts
    2,984
    Quote Originally Posted by NogDog View Post
    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.)
    Thanks for pointing that out....can't help but to love learning new things like this.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  14. #14
    Join Date
    Oct 2012
    Posts
    6
    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...

  15. #15
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    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.
    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.
    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";

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