G
G
grabbee2014-05-29 14:34:19
PHP
grabbee, 2014-05-29 14:34:19

How to organize OOP php

I'm making an access class to GET POST data, it's just like

<?
namespace core;

 class Requests {   
  
public static function GET($VarName, $Default = NULL)
  {
   return isset($_GET[$VarName]) ? $_GET[$VarName] : $Default;
  }

in a separate class I make standard data processing of the type
<?          
namespace contr;

use core\Requests;
 
 class RequestPrepare extends Requests { 
   public static function correctData($data)
   {
     $data = strip_tags( (STRING)$data );
     $data = mysql_real_escape_string ( trim( $data ) );
    return ($data);
   }

   public static function correctInt($data)
   {
     $data = abs( (INT)$data );
    return ($data);
   }

But all incoming data is always checked, numbers through one, lines through the second. Now so
$request = new contr\RequestPrepare ; 
 echo $request->correctInt( $request->GET('str') );

It is right? Or not very...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Lesnykh, 2014-05-29
@grabbee

1. mysql_real_escape_string will not work without a connection resource.
2. forget about CAPSLOCK and read what camelCase is, your code will be more pleasant to read.
3. why static methods?
4. Read about the design pattern (template) - Singleton (singleton), usually the Request object is implemented in this way.
5. Why do we need inheritance in 2 classes?
6. instead of various correct* methods , I would rewrite the getter function like this:

const STRING = 1;
const INT = 2;
// ...
function get($value, $default = null, $type = self::STRING) {
    if (! $value) {
        return $default;
    }

    switch ($type) {
        case self::STRING:
            $value = addslashes(trim(strip_tags($value)));
            break;
        case self::INT:
            $value = (int) $value;
            break;
        default:
            throw new Exception('Unknown type');
            break;
    }

    return $value;
}

P
Pavel Solovyov, 2014-05-29
@pavel_salauyou

it is not clear why you need this, but there are a couple of remarks if the method is declared as static then call it as static

public static function GET

Requests::GET('str');

return($data); Why parentheses?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question