Classes. What's the point?

后端 未结 12 1877
夕颜
夕颜 2020-12-03 03:09

I\'m fairly new to OOP in PHP, I\'ve made a couple of basic scripts but nothing impressive. All I\'ve really taken from it is that it would probably be easier just make a co

相关标签:
12条回答
  • 2020-12-03 03:51

    In PHP, classes can, at the very least, save you a bunch of include statements. The lazy loading of the __autoload() function is very handy. Also, you can be confident that function names in different includes won't conflict.

    To go beyond that, you can start to think of creating an API around a class. Functions which are needed only by other functions within the class can be marked private. And eventually, you should be able to ignore the actual code in your classes, and just think of them as API calls.

    If I call Do::stuff($a, $b) I get this response.

    And that's all you need to know. You can forget how it works "under the hood" inside the class.

    And then, of course, there's classes with non-static functions. These can do various interesting things that normal functions can't.

    0 讨论(0)
  • 2020-12-03 03:54

    While it may at first look simpler to just use a set of functions and include them, classes have their strong points. Classes can store variables and those variables are there "later."

    Here's an edited example from php.net

    <?php
    $item_name = 'Widget 22';
    $item_price = 4.90;
    $item_qty = 2;
    $item_total = ($item_price * $item_qty);
    echo "You ordered $item_qty $item_name @ \$$item_price for a total of: \$$item_total.";
    ?>
    

    v.s:

    <?php
    class Item {
      protected $name, $price, $qty, $total;
    
      function __construct($name, $price) {
        $this->name = $name;
        $this->price = $price;
      }
    
      function calculate($qty) {
        $this->total = number_format(($this->price * $qty), 2);
      }
    
      public function __toString() {
        return "You ordered ($this->qty) '$this->name'" . ($this->qty == 1 ? "" : "s") .
        " at \$$this->price, for a total of: \$$this->total.";
      }
    }
    
    $widget22 = new Item("Widget 22", 4.90);
    
    $widget22->calculate(2);
    
    echo $widget22;
    ?>
    

    Another huge benefit is that you can make more of them. Say you want to calculate the price of another item and print it. Instead of having to duplicate all the fancy logic, you can just call new Item and be done.

    0 讨论(0)
  • 2020-12-03 03:56

    Just a coupl months back I was posting similar questions about classes, I have been using PHP for a few years now and I really didn't see a need to use classes, they just complicated a simple process like you said, well I finally decided to jump in on the Class train and I am still learning but I am comfortable using them now and WOW I have a completely opposite way of thinking about them now, I think they are the best feature in PHP now!

    I will admit they can be overkill for a small application but a large social network site like mine is far from small, it has hundreds of files and now that I am converting over to classes it is structured much better now and easiar to build, update, manage my code and other users could come in and understand my whole site a lot better now that it uses classes. Another great feature is you can break up all those functions you might have into seperate class files and use autoload() function to auto load the correct file only when the class is needed, this is great on a big project too. Classes make things more plug in play.

    All I can say is it's kind of one of those things you need to dive into to see the REAL benefits of it.

    0 讨论(0)
  • 2020-12-03 03:58

    It would definitely be easy to code in procedural programming (that's what your simple process is called) if your website is small. But as your website grow, you will definitely need to sort things.

    For example, you will need classes to act as templates for entities to the database. You might need classes to represent each page, and in a single file you might create multiple pages. Thus these are all advantages of OOP.

    In another example you have like 40 over functions. You would want to group them together. Putting them in a class as static methods would definitely help. It'll clean things up so that in future when you come back to the same code, everything looks familiar to you.

    0 讨论(0)
  • 2020-12-03 03:58

    I always thought the same thing until I started using classes--realizing that it is so much simpler and better. You should create those functions that you're mentioning, but gather them all within a class. The functions can then within a class share the same variables, and therefore making it simpler when having to use the same functions multiple times.

    The best example I can come up with now is connecting to a database.

    Instead of having individual functions that connect, sends a query, fetching the results and closing the connection you could wrap all these functions within a class. This would allow you to communicate to the database multiple times using the same functions, without having to make loads of variables tailored for the specific use on the page.

    0 讨论(0)
  • 2020-12-03 03:58

    If you're just writing procedural-style functions then slapping them together into a class you're not going to get many of the benefits of OOP. I think that the Open Closed Principle and the Dependency Inversion Principle describe some of the biggest benefits of OOP, which you wont get unless you intentionally design them into your code.

    OOP isn't perfectly suited to everything, though. I think procedural programming is well suited to web development. It involves converting a (fairly) simple request into a bunch of HTML, which is a highly procedural task. If it's simpler to include a few functions, there's no point trying to shove an OOP shaped block into a procedurally shaped hole. However, I imagine that OOP would be the better option if the website was very complex.

    0 讨论(0)
提交回复
热议问题