“随机效用函数”的优雅实践

发布于 2024-08-20 08:17:30 字数 122 浏览 10 评论 0原文

你们如何组织随机函数来改进 OOP 类(全局函数)之外的语言功能?

我见过库,但我仍然不相信这是一个好的解决方案,特别是如果你没有足够的功能。我对人们如何组织随机 PHP 和 JavaScript 函数特别感兴趣。

How do you all organize your random functions to improve a language's functionality outside of OOP classes (global functions)?

I've seen libraries, but I'm still not sold on this being a good solution, especially if you don't have enough functions. I'm specifically interested in how people organize random PHP and JavaScript functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里人 2024-08-27 08:17:30

我尝试完全避免在全局命名空间中声明函数。我这样做的极少数情况是添加我的 PHP 版本中不存在的函数的用户态实现时,例如,

if(false === function_exists('lcfirst'))
{
    function lcfirst( $str ) { /* ... */}
}

这样的函数可以放入兼容性.php 中,该文件将包含在引导程序文件中,因此它们可用在整个应用程序中,对 function_exists 的检查可确保一旦 PHP 版本对该函数具有本机支持,我就不会遇到问题。

对于所有其他函数,我会尝试看看它们是否不能首先进入专用对象。通常,“随机”函数只是放错了地方。查看哪些对象使用您的实用函数,然后看看是否可以将方法移到那里。也许有一个超类正在等待出现。另请参阅信息专家模式

如果没有对象可以继续使用这些方法,您仍然可以将它们分组到一个名为 Utils 的静态模块中,并位于唯一的命名空间中,这样它们就不会弄乱全局命名空间。这样,您可以确保不会与全局范围内的其他第三方函数发生冲突。

在 5.3 之前,我会遵循 PEAR 命名约定对它们进行分组并在文件夹结构后添加类名前缀,例如,如果模块位于 com/mattmueller/utils.php 中,则可以使用

class Com_MattMueller_Utils
{
     public static function something($a, $b) { /* ... */ }
}

从 PHP5.3 开始,我们已经有了真正的 命名空间 你可以

namespace com\mattmueller\Utils;

class Utils
{
    public static function something($a, $b) { /* ... */ }
}

在 Javascript 中做,你没有命名空间,但可以 通过将函数添加到对象来轻松模拟它们,例如

// JavaScript
var com = (com) ? com : {};
com.mattmueller = {
    'Utils': {
        'something' : function(a,b) { /* ... */ }
     }
};

常见框架通常实现创建命名空间

I try to avoid declaring functions in the global namespace altogether. The very rare occasions where I do this, is when adding userland implementations of functions that are not in my version of PHP, for instance

if(false === function_exists('lcfirst'))
{
    function lcfirst( $str ) { /* ... */}
}

Functions like this could go in a compatibility.php which would be included in a bootstrap file, so they are available throughout the application and the check to function_exists makes sure I don't run into issues once the PHP version has native support for the function.

For all other functions, I'd try to see whether they cannot go onto a dedicated object first. Usually, "random" functions are just misplaced. Have a look at which objects use your utility functions and then see whether you can move the methods there. Maybe there is a superclass waiting to come out. Also see Information Expert pattern.

If there is no objects these methods can go on, you can still group them in a static module with the name Utils in a unique namespace, so they don't clutter up the global namespace. This way, you can be sure you are not clashing with other third party functions in the global scope.

Prior to 5.3, I'd group them following the PEAR naming convention and prefixing class names following your folder structure, for example if the module was in com/mattmueller/utils.php, you'd use

class Com_MattMueller_Utils
{
     public static function something($a, $b) { /* ... */ }
}

As of PHP5.3, we've got real namespaces and you can do

namespace com\mattmueller\Utils;

class Utils
{
    public static function something($a, $b) { /* ... */ }
}

In Javascript you don't have namespaces but can easily simulate them by adding the functions to an object, e.g.

// JavaScript
var com = (com) ? com : {};
com.mattmueller = {
    'Utils': {
        'something' : function(a,b) { /* ... */ }
     }
};

The common frameworks usually implement functions for creating namespaces as well.

轮廓§ 2024-08-27 08:17:30

我通常为所有奇怪的函数保留一个 functions.phpcommon.php ,这些函数最好首先应该在 PHP 中。 (意思是,根本不特定于我的项目)。

这可能类似于将标准函数扩展到多维数组,或者适合该类别的其他内容。

当我更改项目时,我只需将该文件复制到下一个项目,它就可以轻松地随身携带。然后我只需确保它已加载到我的加载脚本中,并且我已成功扩展了该语言。


对于项目特定的事情,我保留了一个 Misc 类,其中包含非常奇怪的函数调用,同时又是项目特定的。


对于 Javascript 函数,我可以想象同样的事情也适用。如果您想创建 functions.jsglobal.js 类型文件,您可能可以使用相同的逻辑。

I generally reserve a functions.php or common.php for all of my weird functions that should have ideally been in PHP in the first place. (Meaning, not at all specific to my project).

This can be something like making a standard function extend to multi-dimensional arrays, or something else that fits in that category.

When I change projects, I just copy that file over to the next project, and it can easily go with me wherever. Then I just make sure it is loaded in my load script, and I have successfully extended the language.


For project specific things, I keep a Misc class that contains the really weird function calls that are, at the same time, project specific.


For Javascript functions, I can imagine the same thing can apply. If you want to create a functions.js or a global.js type file, you could probably use the same logic.

沧桑㈠ 2024-08-27 08:17:30

我总是使用一个帮助器类,我可以在其中放置所有非 OOP 代码,哈哈。我的意思是,这是助手仍然是面向对象的,并且有方法而不是函数,其优点是您可以在不同的助手中组织函数。如StringHelper、DBHelper等。

I allways use a helper class where I can put all my non-OOP code, LOL. I mean that it's the propourse of helpers still being OO and have methods instead functions, with the advantage that you can organize your functions in diferent helpers. Like StringHelper, DBHelper, etc.

半枫 2024-08-27 08:17:30

对于 Javascript,我发现第一选择应该是将我的实用程序集成到 jQuery 中。它就像编写任何其他类型的函数一样简单,并且当事情变得更加复杂时,能够利用 jQuery 对所有内容(以及站点中所有其他页面特定代码)施加的范例是很棒的。

For Javascript, I've found that the first choice should be to integrate my utilities into jQuery. It's as easy as writing any other sort of function, and when things get more complicated it's great to be able to leverage the paradigm that jQuery imposes over everything (and over all my other page-specific code in the site).

纸短情长 2024-08-27 08:17:30

在 JavaScript 中,创建一个新文件并将它们分组到一个对象

global.js 下:

/* Function definitions */ 
var myFunctions = new Object();
myFunctions.func = function () {
   alert("hello"); 
}

同样的想法也可以用于 PHP。有了这个,当你的程序变得更大时,你就不需要担心命名约定的冲突。

In JavaScript, make a new file and group them under an object

global.js:

/* Function definitions */ 
var myFunctions = new Object();
myFunctions.func = function () {
   alert("hello"); 
}

Same idea can be used for PHP. With this, you don't need to worry about conflicts in naming conventions when your program grows bigger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文