想要创建自定义函数

编程入门 行业动态 更新时间:2024-10-26 04:21:40
本文介绍了想要创建自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用CakePHP 2.x ..我想创建一个特殊的类,其中我想创建函数,以便我可以从其他控制器调用函数。

I am working on a CakePHP 2.x .. I want to create a special class in which I want to create functions so that I can call functions from other controllers.

例如此函数

function replace_dashes($string) { $string = str_replace("-", " ", $string); return $string; }

所以每当我想在其他控制器中使用该函数时,或者可以传递参数...

So whenever I want to use that function in some other controller I can call this or can pass parameters too ...

我想在一些类中实现所有的原始函数。如何在CakePHP中这样做?

I want to implement all the raw functions like this in some class. How can I do this in CakePHP?

推荐答案

只需在/ Lib中创建一个文件,最好使用Utility这样的命名空间:

Its not that difficult. Just create a file in /Lib, ideally with a namespace like "Utility":

/Lib/Utility/Utility.php

并将您的方法放入其中:

and put your methods in there:

class Utility { public static function replaceDashes($string) { ... } }

然后你可以在你的应用程序的任何地方使用它:

Then you can use it anywhere in your app:

//App::uses('ClassName', 'Package'); and our Package is the Folder "Utility" in /Lib App::uses('Utility', 'Utility'); $result = Utility::replaceDashes($input);

github/dereuromark/tools/blob/master/Lib/Utility/Utility.php =nofollow> github/dereuromark/tools/blob/master/Lib/Utility /Utility.php 及其对于现实生活场景/示例的测试用例。

See github/dereuromark/tools/blob/master/Lib/Utility/Utility.php and its test case for a real life scenario/example.

不要忘记写几个测试用例,以及。

Don't forget to write a few test cases for, as well.

更多推荐

想要创建自定义函数

本文发布于:2023-07-14 22:12:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!