Tutorial :Functions outside the class



Question:

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?


Solution:1

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


Solution:2

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


Solution:3

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.


Solution:4

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


Solution:5

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »