为什么在基本功能上何时使用PHP OOP?何时使用? [英] Why use PHP OOP over basic functions and when?

查看:52
本文介绍了为什么在基本功能上何时使用PHP OOP?何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些关于此问题的文章,但是我并不清楚在何时在include中使用何时使用面向对象的编码以及何时使用编程功能.有人还向我提到,OOP运行起来很繁重,并且增加了工作量.是这样吗?

比方说,我有一个包含50个功能的大文件.我为什么要在课堂上称呼这些?而不是通过function_name()吗?我应该切换并创建一个包含我所有功能的对象吗?优势或具体区别是什么?它给PHP代码OOP带来什么好处?模块化吗?

解决方案

在很多情况下,过程编程都很好.为了使用OO而使用OO是没有用的,尤其是如果您最终要使用 POD 对象(纯旧数据).

OO的功能主要来自继承和多态性.如果您使用类,但从不使用这两个概念中的任何一个,则可能不必首先使用类.

OO所代表的IMO最好的地方之一是允许您摆脱接通型代码.考虑:

function drive($the_car){

    switch($the_car){

      case 'ferrari':
          $all_cars->run_ferrari_code();
          break;

      case 'mazerati':
          $all_cars->run_mazerati_code();
          break;

      case 'bentley':
          $all_cars->run_bentley_code();
          break;
    }
}

及其OO替代方法:

function drive($the_car){

    $the_car->drive();
}

基于运行时信息,多态将允许适当类型的驾驶"发生.


关于多态性的说明:

这里的第二个示例有一些前提:即所有汽车类都将扩展接口.

两者都允许您强制扩展或实现类以定义特定功能,例如drive().这非常强大,因为它可以让您drive()所有汽车而不必知道您驾驶的是哪一辆.这是因为他们正在扩展包含drive()方法的抽象类或实现强制定义drive()方法的接口.

只要确保所有特定的汽车都扩展了抽象类car或实现了诸如canBeDriven的接口(两者都必须 declare drive()方法) )您可以在已知是汽车(而不是汽车的类型)的对象上调用drive()方法,而不必担心未定义它,因为PHP会在您定义这些方法之前向您抛出致命错误.特定的汽车类别.

There are some posts about this matter, but I didn't clearly get when to use object-oriented coding and when to use programmatic functions in an include. Somebody also mentioned to me that OOP is very heavy to run, and makes more workload. Is this right?

Let's say I have a big file with 50 functions. Why will I want to call these in a class? And not by function_name()? Should I switch and create an object which holds all of my functions? What will be the advantage or specific difference? What benefits does it bring to code OOP in PHP? Modularity?

解决方案

In a lot of scenarios, procedural programming is just fine. Using OO for the sake of using it is useless, especially if you're just going to end up with POD objects (plain-old-data).

The power of OO comes mainly from inheritance and polymorphism. If you use classes, but never use either of those two concepts, you probably don't need to be using a class in the first place.

One of the nicest places IMO that OO shines in, is allowing you to get rid of switch-on-type code. Consider:

function drive($the_car){

    switch($the_car){

      case 'ferrari':
          $all_cars->run_ferrari_code();
          break;

      case 'mazerati':
          $all_cars->run_mazerati_code();
          break;

      case 'bentley':
          $all_cars->run_bentley_code();
          break;
    }
}

with its OO alternative:

function drive($the_car){

    $the_car->drive();
}

Polymorphism will allow the proper type of "driving" to happen, based on runtime information.


Notes on polymorphism:

The second example here has some premisses: That is that all car classes will either extend an abstract class or implement an interface.

Both allow you to force extending or implementing classes to define a specific function, such as drive(). This is very powerful as it allows you to drive() all cars without having to know which one you're driving; that is because they're extending an abstract class containing the drive() method or implementing an interface forcing the drive() method to be defined.

So as long as you make sure that all your specific cars either extend the abstract class car or implement an interface such as canBeDriven (both of which must declare the drive() method) you can just call the drive() method on an object which you know is a car (but not what type of car) without fear of it not being defined, as PHP will throw fatal errors at you until you define those methods in your specific car classes.

这篇关于为什么在基本功能上何时使用PHP OOP?何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆