Mindaugas Posted January 10, 2010 Share Posted January 10, 2010 Hello everyone, I want to know how to get thru one problem i have. I`m learning php, I`ve learned functions but i never used it, learned arrays, but doesn`t used it too.. When i want to do something i`m doing it without it, but i`m reaching same result. Is it bad? For example i can : function date() { echo date("Y-m-d") } But instead i`m just using : echo date("Y-m-d"); Or many other examples. I prefer use more lines but don`t use functions. I don`t know why I do so, and i wonder is it bad ? Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/ Share on other sites More sharing options...
trq Posted January 10, 2010 Share Posted January 10, 2010 Your example doesn't really make allot of sense because all that function does is wrap around another simple function. Functions however come in handy when you want to wrap more logic together into a re-usable piece of code. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992074 Share on other sites More sharing options...
Mindaugas Posted January 10, 2010 Author Share Posted January 10, 2010 Any small examples when functions come in handy, please ? Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992080 Share on other sites More sharing options...
trq Posted January 10, 2010 Share Posted January 10, 2010 Whenever you need to wrap logic into re-usable code. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992083 Share on other sites More sharing options...
Mindaugas Posted January 10, 2010 Author Share Posted January 10, 2010 But still if i can do it without function, is it bad if I do so ? Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992089 Share on other sites More sharing options...
trq Posted January 10, 2010 Share Posted January 10, 2010 Using a function makes it re-usable. Otherwise, you need to keep re-writing code. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992092 Share on other sites More sharing options...
Mindaugas Posted January 10, 2010 Author Share Posted January 10, 2010 I think i`ll notice that when i get bored of rewriting code or i will notice that i`m rewriting it Anyway thx for answers Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992096 Share on other sites More sharing options...
nrg_alpha Posted January 10, 2010 Share Posted January 10, 2010 Constantly re-writing code is a violation of the DRY principal. Take into consideration.. suppose later one you want to change the format from "Y-m-d" to say "Y-d-m" and have that change propogated throughout your page? Fast and easy maintenance will be problematic as it currently stands, as you have to find every hard coded instance and change those. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992294 Share on other sites More sharing options...
Mindaugas Posted January 11, 2010 Author Share Posted January 11, 2010 But for example if i have 200+ files , and they all have date function, so i still have to go thru all files and rewrite code Then i must use include() instead of functions ? to change one file and it will change in all files. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992827 Share on other sites More sharing options...
Lamez Posted January 11, 2010 Share Posted January 11, 2010 See I don't have that problem. I have a functions page, a header page, and a footer page. Then I have a main page to tie them all together. Then I include the main page on the user pages. So when I add a function or change something in a function or even the header, then I don't have to go back and change every single page. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992845 Share on other sites More sharing options...
nrg_alpha Posted January 11, 2010 Share Posted January 11, 2010 Well, you could use an include, sure.. but you can also make use of [abbr=Object Oriented Programming]OOP[/abbr] and build a class, which you can then instantiate from any file (using an __autoload() function in your pages, you can point to a folder containing your classes, and all the classes you use in your page will be automaticalled loaded - so you don't need to manually include all of them individually). The whole idea of this is to provide easy maintenance of commonly used separate files. Separate files are decoupled from the main page, and so long as these files do a specific singular task, you are also employing the Separation of concerns principal. As it currently stands, you are hardcoding everything into your pages.. which again will make maintenance a nightmare. EDIT - Lamez, you just posted before I did.. so my comment is directed to Mindaugas, just so there is no confusion. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-992854 Share on other sites More sharing options...
Mindaugas Posted January 12, 2010 Author Share Posted January 12, 2010 I`m very confused now .. Classes for now is a sky for me But more practise and i`ll try make better scripts Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-993401 Share on other sites More sharing options...
Lamez Posted January 12, 2010 Share Posted January 12, 2010 I don't see the point in classes. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-993749 Share on other sites More sharing options...
nrg_alpha Posted January 12, 2010 Share Posted January 12, 2010 I don't see the point in classes. I'm going to guess that you have not worked with classes often then (or at all).. OOP offers plenty of advantages over their procedural counterpart. These advantages include abstraction, encapsulation, inheritence, composition, etc.. Objects (a data type that is the result of class instantiation) offers robust capabilities. Once you start learning OOP, you'll realize its benefits. This is not to suggest that everything under the sun use OOP.. as that paradigm has its place in PHP programming, much like procedural has its own place was well. But when the circumstances call for OOP, its a great system to know and employ. OOP by its very nature is a large and tricky concept for the uninitiated to learn.. so in the event you are willing to have a looksie, there are some tutorials out there (including this site - John Kleijn, one of our members, has written some OOP tutorials to help get things kickstarted - just visit our home page tutorial section.. Won't take long to find them). Also, Java has its own tutorials on OOP: http://java.sun.com/docs/books/tutorial/java/concepts/index.html While it isn't PHP, the concepts are pretty much applicable. And apparently a highly recommended PHP OOP book is this one. In either case, I would choose OOP over procedural functions tucked away in include files anyday. [ot] For the record, with regards to this thread, when I mention classes in this case, I am not referring to entire chunks of code in include files for site headers, footers and whatnot. I'm talking about creating small, focused / specific classes with specific purposes in mind. [/ot] Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-993818 Share on other sites More sharing options...
Lamez Posted January 12, 2010 Share Posted January 12, 2010 As I have worked with classes before (Java, not PHP), I still have not seen a point yet. However, I am always willing to try something new. I have a form processes page that I have to edit every time I create a major form (login, register, etc) so maybe when I get done with the login system, I will go back and edit it. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-993846 Share on other sites More sharing options...
Daniel0 Posted January 12, 2010 Share Posted January 12, 2010 I don't see the point in classes. That's because Object-Oriented Programming is not about classes but... *drumroll* objects And just to make it clear, you are not doing OOP just because you've instantiated an object. If you merely use it as a wrapper/namespace for your functions, then yes, it's entirely pointless. But then again, that's not the point of OOP. Quote Link to comment https://forums.phpfreaks.com/topic/187897-problem-that-i-have/#findComment-993849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.