-1

Initially I had the habbit of directly writing the php code in the

<?php

?>

region. Those were for simpler sites and lesser traffic.

Now I am in process of developing a bigger site involving a lot of social networking. So would it be apt to leave the code just like that or should I encapsulate it.

Are there any disadvantages or chances of the site crashing if the code is not in any function/class?

Shivang
  • 186
  • 2
  • 11
  • 1
    Not all PHP code is a function or class. Can you give an example of your code? Not real sure what you're talking about. – Jonathan M Jun 27 '12 at 15:57
  • Site crashing? No. Hardly maintainable? Oh yeah. – netcoder Jun 27 '12 at 16:01
  • If you are developing a bigger site then habit of mixing html and php will give a situation like - "I started this code and me and only god knew what I was doing; now only god knows" :). Better learn about PHP frameworks. – Shubham Jun 27 '12 at 16:02
  • Nice one! No the code is quite easily understandable its just that I've always had the habbit of coding in this way :) Though I guess would need to change it as soon as possible – Shivang Jun 27 '12 at 17:05

2 Answers2

2

Create classes and functions for the sake of creating classes and functions? No.

Create functions to support code reuse? Yes. Create objects to adopt one of several possible code patterns? Yes.

I suggest a good deal of reading to gain an understanding of the fundamental difference between procedural and OOP programming.

Start with this question, which isn't technically a duplicate, but pretty close: simple explanation PHP OOP vs Procedural?

Read the answers, especially the detailed one and the one with all the links -- visit the links.

Check out tutorials like this one or this one. You must understand not only the difference between the programming styles, but also when and why to use them. Simply dashing functions and classes into your code so you can "do OOP" is not actually using OOP at all, it's just making your code into spaghetti.

Community
  • 1
  • 1
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
1

The disadvantages a huge regarding:

  • code readability: now as well as for the future, for you as well as for any future developers that might work on the project;
  • maintenance: changing the same code in 10 different files might mean you forgot to change it in the 11th file;
  • verbosity: writing the same stuff tens of times just because the code isn't centralised / organised into reusable pieces (one can argue that reusable pieces can be achieved by structuring the files and including them);
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • No actually my code is based as per page. I was a little misguided when one of my friends told me the site would crash if we leave the code like that and hence my confusion – Shivang Jun 27 '12 at 16:09
  • 1
    One rule of business, if you stick a nail in the wall, it won't just fall out without external intervention. It doesn't matter if it was beaten into the wall with a hammer or a microscope. – Mihai Stancu Jun 27 '12 at 16:10
  • 1
    It's true that under high-load, huge numbers of visitors, etc. it could fail. But not *because* you wrote it in a procedural manner. Most likely it would fail because it has some other flaws such as server limits, caching limits, or database connection limits. But that's also true of any OOP application that was not designed to face these issues. – Mihai Stancu Jun 27 '12 at 16:12
  • Thanks @Mihai that clearly gives what I wanted. So in order to avoid database connection limit conflict I have just included the connect to database code in every file. I guess that should be enough? – Shivang Jun 27 '12 at 17:06
  • One last thing is that in order to be a good programer or software engineer etc. you have to know many possible approaches to solving a problem and choosing the best one. Some fields of work just do better with procedural programming (microprocessors for example) others with OOP (many examples) some do best with the functional approach (artificial intelligence). – Mihai Stancu Jun 27 '12 at 19:08