Search code examples
phpmodel-view-controlleroop

php data classes?


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)


Solution

  • 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;
            }
        }
        */
    
    }