• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Objects from custom classes

#1
[eluser]kRON[/eluser]
Hey everyone! Just started using CI today and having a blast. There's one thing I'm bugged though and I can't really find it in the user guides.

I want to know how do you hook your custom classes with, let's say, your model classes? To be more specific, I'm trying to make my Twitter model that would cache and parse my twitter feed. Specifically, to make my life easier, I'd like to have the Twitter model return Twit
objects that contain all the data.

I'm not that versed with OOP in PHP, so this is, kinda, how I've imagined everything

Twitter model
Code:
class Twitter extends Model {
    
    // ...
    
    function return_latest() {
        for each(...) {
            // ...
            $result [] = new Twit(...);
        }
        
        return $result;
    }
    
}

Twit class
Code:
class Twit {
        
        // ...
        
        function Twit(...) {
            $this->...
        }
        
        function get_... {
            return ...
        }
    }

Controller
Code:
$this->load->model('Twitter');
// ...
$data['twitters'] = $this->twitter->get_latest();
// ...

View
Code:
<?php foreach ($twitters as $twit): ?>
        ... <?=$twit->get...();?> ...
<?php endfor; ?>

Can anyone help me explain where and how would I create the Twit class as to make it available for creating objects within the Twitter model? I hope I'm not thinking about doing this the wrong way and that something like this is possible - is there a better practice?

#2
[eluser]crumpet[/eluser]
The way i deal with this is to create a library called lib_twit.php
in that file there are two class declarations
Code:
class lib_twit{
   function create(){
       return new twit();
   }
}
class twit{
//this is your class
}
Then when you need to use it you go
Code:
$this->load->library('lib_twit');
foreach(...){
   $return[] = $this->lib_twit->create();
}
return $return;

#3
[eluser]kRON[/eluser]
So I could do it with multiple classes per document? I thought about it, but seemed very unOOPish Big Grin! Right, thanks, I'll have a jab at it right away.

#4
[eluser]kRON[/eluser]
Thanks crumpet, works like a charm! So I guess that wraps it up, factory patterns are the way to go.

Just in case someone bumps into the same problem, here is my quick 'n dirty test Model forwarding custom objects

Code:
class Twitter extends Model {
    
        private $dom;
        private $data;
        private $screen_name;
        private $author_image;
        
        function Twitter() {
            parent::Model();
            $this->load->helper('rest_request');
            
            # Create DOMDocument from XML
            $this->dom = new DOMDocument();
            $this->dom->load(rest_request('http://twitter.com/statuses/user_timeline/kron_.xml?count=5', 'document', 300));
            
            # Fetch screen name and image
            $node = $this->dom->getElementsByTagName('status')->item(0)->getElementsByTagName('user')->item(0);
            $this->author_image = $node->getElementsByTagName('profile_image_url')->item(0)->nodeValue;
            $this->screen_name = $node->getElementsByTagName('screen_name')->item(0)->nodeValue;
        }
        
        public function get_latest() {
            $result = array();
            $statuses = $this->dom->getElementsByTagName('status');
            foreach ($statuses as $status) {
                $result [] = new Twit(
                    $status->getElementsByTagName('text')->item(0)->nodeValue,
                    $status->getElementsByTagName('created_at')->item(0)->nodeValue,
                    $status->getElementsByTagName('source')->item(0)->nodeValue
                );
            }

            return $result;
        }
        
        public function get_screen_name() {
            return $this->screen_name;
        }
        
        public function get_author_image() {
            return $this->author_image;
        }
        
        
    }
    
    class Twit {
        private $text;
        private $date;
        private $source;
        
        public function Twit($text, $date, $from) {
            $this->text = $text;
            $this->date = format_date($date);
            $this->from = $from;
        }
        
        private function format_date($date) {
            // TODO format date
            return $date;
        }
        
        # Twit HTML render
        public function get_data() {
            // TODO format output
            return '<p class="twitter-text">' . $this->text . '</p>';
        }
        
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.