Jump to content

Problem that i have


Mindaugas

Recommended Posts

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 ?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.