/    Sign up×
Community /Pin to ProfileBookmark

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 [url]http://us3.php.net/manual/en/book.classobj.php[/url] but nothing really usefull

to post a comment
PHP

23 Comments(s)

Copy linkTweet thisAlerts:
@NihilisteOct 06.2009 — 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
Copy linkTweet thisAlerts:
@MindzaiOct 06.2009 — There is no "better", they both serve different purposes.
Copy linkTweet thisAlerts:
@NogDogOct 06.2009 — 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.

[url=http://www.ibm.com/developerworks/opensource/library/os-phpobj/]Getting started with objects with PHP V5[/url]

[url=http://www.ibm.com/developerworks/opensource/library/os-advphpobj/?ca=dgr-lnxw06advancedPHP5]Advanced PHP V5 objects[/url]

And consider getting your hands on [i][url=http://www.apress.com/book/view/1590599098]PHP Objects, Patterns, and Practice[/url][/i] by the same author (Matt Zandstra).
Copy linkTweet thisAlerts:
@opifexOct 06.2009 — There is no "better", they both serve different purposes.[/QUOTE]
This is 100% correct.
A class may contain its own constants, variables (called "properties"), and functions (called "methods").[/QUOTE] ~ php.net

In this case we have apples, oranges and bananas - and that really is what we want - [I]PHP Fruit Salad[/I], so we can get the job done as efficiently as possible! ?
Copy linkTweet thisAlerts:
@wetubeSep 10.2011 — 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 ?
Copy linkTweet thisAlerts:
@Jeff_MottSep 10.2011 — I suspect that the distinction the OP means to make is procedural vs OO.
Copy linkTweet thisAlerts:
@iansane6Sep 11.2011 — 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.
Copy linkTweet thisAlerts:
@aj_nscSep 11.2011 — 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.
Copy linkTweet thisAlerts:
@NogDogSep 11.2011 — 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]


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.)
Copy linkTweet thisAlerts:
@iansane6Sep 11.2011 — 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.
Copy linkTweet thisAlerts:
@NogDogSep 11.2011 — 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]


You could pretend you are using an OO-only language (e.g. Java) where everything [i]has[/i] 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 [I]few[/I] lines of code that are not part of a class in order to load and instantiate the "main" class. ?
Copy linkTweet thisAlerts:
@aj_nscSep 11.2011 — 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]


Thanks for pointing that out....can't help but to love learning new things like this.
Copy linkTweet thisAlerts:
@paulens12Dec 27.2012 — 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...
Copy linkTweet thisAlerts:
@Jeff_MottDec 27.2012 — actually i don't see any reason to use classes. you can do ABSOLUTELY the same with plain functions.[/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.
Copy linkTweet thisAlerts:
@NogDogDec 27.2012 — Similarly, you can do the same thing without functions: just make generous use of copy-and-paste, and perhaps some judicious use of GOTO. :-)
Copy linkTweet thisAlerts:
@hastxDec 28.2012 — 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]

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
Copy linkTweet thisAlerts:
@paulens12Dec 28.2012 — 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 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 [i]absolutely[/i] 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.
Copy linkTweet thisAlerts:
@Jeff_MottDec 28.2012 — 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.
Copy linkTweet thisAlerts:
@paulens12Dec 28.2012 — 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.

iwewe
Copy linkTweet thisAlerts:
@paulens12Dec 28.2012 — 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[/QUOTE]
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...
Copy linkTweet thisAlerts:
@paulens12Dec 28.2012 — 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...
Copy linkTweet thisAlerts:
@hastxDec 28.2012 — 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]

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

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.

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

[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]


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:
[LIST]
  • [*]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.

  • [/LIST]



    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]

    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.
    Copy linkTweet thisAlerts:
    @NogDogDec 29.2012 — yeah and you'll have a nice 10 000 lines file... thanks, no...[/QUOTE]

    Sorry: apparently I should have wrapped my reply in [sarcasm]...[/sarcasm] tags. ?
    ×

    Success!

    Help @moiseszaragoza spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...