tags:

views:

70

answers:

5
+1  A: 

Yes, just define your function outside of a class definition.

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

Then call it like this

safe_query($string);
Daniel Von Fange
Damn, I promise I tried that and it didn't work and now it perfectly works 0.o Thanks
hey
+1  A: 

Is there any way to make this sentence : echo functions::safe_query($value); prettier?

Not really. IMO having a functions class serves no purpose, simply make it a global function (if it's not part of a more logical class, such as Database) so you can do safe_query($value); instead.

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

Create files for logical blocks of code, not for what type of code it is. Don't create a file for "functions", create a file for "database related code".

Krevan
+2  A: 

Functions outside a class litter the global namespace, and it's an open invitation to slide back to procedural programming. Since you're moving to the OOP mindset, functions::safe_query($value); is definitely prettier (and cleaner) than a function declared outside a class. refrain from using define() too. but having a functions class that's a mix of unrelated methods isn't the best approach either.

stillstanding
+1, but why is it bad approach? If I have to use the same function in a few classes, then I need to create functions over again and again in each class? Is it better?
hey
why do u need to create functions repeatedly when you can call functions::safe_query($value); as many times as you wish?
stillstanding
No, you told: `but having a functions class that's a mix of unrelated methods isn't the best approach either.`, so why is it not the best approach?
hey
although you can, it doesn't make sense combining, say a method that authenticates a user, and a method that generates a google map.
stillstanding
Honestly, before using classes as container of unrelated functions, I would prefer good old functional programming. Just putting functions in a class is *not* OOP and does not help to understand the concept either. @hey: First learn *what* OOP is and then make use of classes. Currently you are misusing classes for some kind of namespacing.
Felix Kling
Thank you, Felix. I don't use classes and OOP in my projects yet, I just practice, trying to understand that and it is really hard to me.
hey
A: 

Starting with OOP can be a real challenge. One of the things I did was looking at how things were done in the Zend Framework. Not only read the manual (http://www.framework.zend.com/manual/en/zend.filter.input.html, but also look at the source code. It will take some effort but it pays of.

Looking at the context of your question and the code example you posted, I would advice you to look at some basic patterns, including a simple form of MVC, and the principles they are based upon.

koen
+1  A: 

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.)

banzaimonkey