duanhuang2804 2010-07-11 22:25
浏览 59
已采纳

程序编程的设计模式和封装?

I'm working on a fairly large PHP project written in a procedural style (it was written before PHP 5), and I can't help but feel that some of the things I'm doing are a little "hackish." A modification somewhere else can easily break the application. All the design patterns and best practices I've seen seem to only apply to OOP. I could start writing some of my code using PHP 5's OOP features, but I'm not sure if all the other developers are familiar enough with OOP.

Is it just the nature of procedural programming to seem "hackish" to people more familiar with OOP? Are there "best practices" books that deal with how to keep large procedural applications maintainable and make the introduction of new bugs less likely?

I know I could apply OOP design principles/patterns in a procedural way, but if I was going to do that, I might as well just use PHP's OOP features. Maybe I just don't have a good enough understanding of the procedural paradigm?

  • 写回答

2条回答 默认 最新

  • dongxun3777 2010-07-11 22:58
    关注

    Procedural programming, especially in PHP, doesn't have a concrete notion of "encapsulation" -- everything's available, it's just not your job to modify it, so you don't. To those who don't know anything but OOP, or were taught that procedural code is BAAAAAAD, yes, it can look hackish and wrong. But people have been doing it for a LONG time, and it does work.

    Now, it's just as likely that you've found some of the ACTUALLY bad procedural code. There's as much of it as there is bad OOP code.

    Basic practices for procedural code aren't a whole lot different than for OOP -- avoid globals if possible, group related functions together and try to keep them short. There aren't really "patterns", per se, as procedural programming predates the patterns movement by a few decades. But clean procedural code need not be as ugly as the OOP zealots would have you believe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等