March 1, 2012

php data classes?

Question by mazzzzz

I’ve learned most of my OOP practices from C#, and coded php primarily in MVC but without custom objects for data types (user information and such I primarily stored in associative arrays). I’m wanting to take what I’ve learned in C# (making structs, or data classes basically, to store specific types of data, such as a user record, or say an article’s information) into php. This is so I can know exactly what data values to expect (because they’re defined fields, instead of just added to an array), allowing a bit more abstraction between controller and view.

Is there a certain way to do this in php, and in the MVC design pattern specifically? I am curious where I should put the definitions of these new “data types.”
Or am I just thinking about this in the wrong way, and there is a better way to do it?

EDIT:
A C# example of what I’m trying to accomplish:

class program1
{
    public void main ()
    {
        Article randomArticle = getArticle (); //instead of returning an object array, it returns a defined data type with predefined fields, so I don't need to guess what's there and what isn't
        Console.WriteLine(randomArticle.title);
        Console.ReadLine();
    }
    public Article getArticle ()
    {
        return new Article("Sometitle", "SomeContent");
    }
}
struct Article
{
    public Title {get; private set;}
    public Content {get; private set;}

    public Article (string title, string content)
    {
        this.Title = title;
        this.Content = content;
    }
}

(idk if the above actually would compile, but it gives you the gist of what I’m attempting to do in PHP)

Answer by zvnty3

In PHP, you are free to write your own systems that accomplish MVC. Instead of rolling your own to start, though, I suggest looking into using an existing system. There’s a huge ecosphere of us php types, and a long history. By some opinions, PHP is more well-established than a younger and faster evolving C#. It’s also simpler, which is nice. Specifically, though: CakePHP is one I’d recommend. Drupal is more robust. And there’s always Zend. Advantage of going Zend is the end-to-end solution from the editor to the server optimization and security.

p.s. c# is more mvvm

EDIT: code example

class Article {

    protected $_title;
    protected $_content;

    public function setTitle( $title ) {
        $this->_title = $title;
    }
    public function setContent( $content ) {
        $this->_content = $content;
    }

    public function getTitle() {
        return $this->_title;
    }
    public function getContent() {
        return $this->_content;
    }

    /* magic not advisable; a technically valid technique, though it can lead to problems
    public function __get( $property ) {
        switch( $property ) {
            case 'title': return $this->_title; break;
            case 'content': return $this->_content; break;
            default: break;
        }
    }
    */

}

Answer by Starx

PHP is free of any logic you would like to implement. IMO, Ensuring data-types leads to a field of validations. I want you to check few terms, that will give you everything required.

  1. Type Casting/ Juggling [docs here]

    Casting is probably fastest way to stop unwanted input. The casts allowed are:

    • (int), (integer) – cast to integer
    • (bool), (boolean) – cast to boolean
    • (float), (double), (real) – cast to float
    • (string) – cast to string
    • (array) – cast to array
    • (object) – cast to object
    • (unset) – cast to NULL (PHP 5)
  2. PHP Filters

    PHP has a function filter_var (http://www.php.net/manual/en/function.filter-var.php) will helps you to compare a data type

    var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
    var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
    
  3. PHP inbuild() functions

    They are many functions in php, which can help you check the data types

    • is_array(): checks array
    • is_integer(): Check integer
    • is_object(): Check objects
    • is_float(): Checks Floating point number
    • ctype_digit(): Checks Integer

    and many more

  4. Regex Validation

    You will be able to find many resources on this on the internet.


Other Resources


Update

Your above method might turn into something like this

public function Article($title, $content) {
    if(is_string($title)) { $this -> title = $title; }
    if(is_string($content)) { $this -> content = $content; }
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!