类外的函数 [英] Functions outside the class

查看:158
本文介绍了类外的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想告诉你,我是OOP的新手,这对我很难,但这里是我的代码:

  class functions 
{
function safe_query($ string)
{
$ string = mysql_escape_string(htmlspecialchars($ string));
return $ string;
}
}

class info
{
public $ text;

function infos($ value)
{
echo functions :: safe_query($ value);
}
}

有什么方法可以使这句话: echo functions :: safe_query($ value); 更漂亮?我可以使用 extends ,比我可以写 echo $ this-> safe_query($ value); 它是最好的办法吗?谢谢。



编辑,也许我甚至可以不使用类函数,只是制作单独的函数文件, ?

解决方案

使用函数类是完全正常的,但它可能不是设计应用程序的最佳方式。

例如,你可能有一个类似这样的静态方法的泛型'string'或'data'类(显然实现失败):

  class strfunc {
public static function truncate($ string,$ chars);
public static function find_prefix($ array);
public static function strip_prefix($ string);
public static function to_slug($ string); #strtolower + preg_replace

}

这是为您提供通用的算法解决方案的集合,您将在应用程序的不同部分重复使用。将这些声明为静态的方法会消除它们的功能性,并且意味着它们不会附加到任何



另一方面,一些行为,例如对于查询的转义数据,更特定于一组特定的数据。在这种情况下可能更适合写这样:

  class db_wrapper {
public function __construct ($ params); #connect to db
public function escape($ string);
public function query($ sql);
public function get_results();
}

在这种情况下,您可以看到所有方法都与数据库对象。您可以稍后将此对象用作需要访问数据库的另一个对象的 part



OOP的本质是保持数据及其相关的行为(方法),称为对象。将行为和数据置于同一位置,可以更轻松地控制数据,方法是确保附加到数据的行为是仅允许更改行为的行为(这被称为 封装)。



此外,在一个地方拥有数据和行为意味着您可以轻松地将该对象(数据和行为)传递到应用程序的不同部分,从而提高代码重用率。其格式为作曲继承



如果你对一本书感兴趣, =http://rads.stackoverflow.com/amzn/click/0672330164 =nofollow noreferrer> 面向对象的思考过程 有利于阅读。或者,您可以查看免费的 面向对象构建技能SO的 S.Lott 设计 。 (提示:PHP语法比Java更类似于Python。)


I just want to tell you that I am newbie to OOP and it is quite hard to me, but here is my code:

class functions
{
    function safe_query($string)
    {
        $string = mysql_escape_string(htmlspecialchars($string));
        return $string;
    }  
}

class info
{
    public $text; 

    function infos($value)
    {
        echo functions::safe_query($value);
    }
}

Is there any way to make this sentence : echo functions::safe_query($value); prettier? I can use extends, than I could write echo $this->safe_query($value);, but is it a best way? Thank you.

edit: and maybe I even can to not use class functions and just make separate file of functions and include that?

解决方案

Using a functional class is perfectly fine, but it may not the best way to design your application.

For instance, you might have a generic 'string' or 'data' class with static methods like this (implementation missing, obviously):

class strfunc{
    public static function truncate($string, $chars);
    public static function find_prefix($array);
    public static function strip_prefix($string);
    public static function to_slug($string); #strtolower + preg_replace
    etc.
}

The point of a class like this is to provide you with a collection of generic, algorithmic solutions that you will reuse in different parts of your application. Declaring methods like these as static obviates their functional nature, and means they aren't attached to any particular set of data.

On the other hand, some behaviors, like escaping data for a query, are more specific to a particular set of data. It would probably be more appropriate to write something like this, in that case:

class db_wrapper{
    public function __construct($params); #connect to db
    public function escape($string);
    public function query($sql);
    public function get_results();
}

In this case, you can see that all of the methods are related to a database object. You might later use this object as part of another object that needs to access the database.

The essence of OOP is to keep both the data and its relevant behavior (methods) in one place, called an object. Having behavior and data in the same place makes it easier to control data by making sure that the behavior attached to the data is the only behavior allowed to change it (this is called encapsulation).

Further, having the data and behavior in one place means that you can easily pass that object (data and behavior) around to different parts of your application, increasing code reuse. This takes the form of composition and inheritance.

If you're interested in a book, The Object-Oriented Thought Process makes for a decent read. Or you can check out the free Building Skills in Object-Oriented Design from SO's S.Lott. (Tip: PHP syntax is more similar to Java than Python.)

这篇关于类外的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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