3

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?

3
  • Side note: mysql_escape_string() is deprecated and mysql_real_escape_string should be used instead. Since you're moving into realm of OOP, perhaps you should also look into mysqli classes: php.net/manual/en/book.mysqli.php
    – Mchl
    Jul 31, 2010 at 11:50
  • (related) Learning PHP class
    – Gordon
    Jul 31, 2010 at 12:28
  • If you want to call the method statically, it should be defined public static function safe_query() or just static function safe_query() if you prefer. Jul 31, 2010 at 20:17

5 Answers 5

8

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);
1
  • Damn, I promise I tried that and it didn't work and now it perfectly works 0.o Thanks Jul 31, 2010 at 11:36
2

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

2

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.

6
  • +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? Jul 31, 2010 at 11:50
  • why do u need to create functions repeatedly when you can call functions::safe_query($value); as many times as you wish?
    – bcosca
    Jul 31, 2010 at 11:53
  • 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? Jul 31, 2010 at 11:56
  • 1
    although you can, it doesn't make sense combining, say a method that authenticates a user, and a method that generates a google map.
    – bcosca
    Jul 31, 2010 at 11:58
  • 2
    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. Jul 31, 2010 at 12:07
1

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

0
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.