tags:

views:

138

answers:

5
class MyImageTestClass{  
    var $url = "nfl2.jpg";  
    var $ID = "one";  

    function __construct() {  

    }  

    function setUrl($value){  
        $this->url = $value;  
    }  

    function setID($newid){  
        $this->ID = $newid ;  
    }  

    function loadImg($value,$newid){  
        $this->setID($newid);  
        $this->setUrl($value);  
        print "<img id=\"$this->ID\" src=\"$this->url\"/>";  
    }  
}  

$n = new MyImageTestClass();  

$n->loadImg("peterson.jpg","two");  

Or should I keep the setID() and setURL out of the loadImg function and then write,

$n->setID();  
$n->setUrl();  
$n->loadImg();  
+1  A: 

You can go for either way; the main thing is that all your functions remain inside the class itself. So there is no problem with any way you go with.

But from your code, i suspect you need just one function:

function loadImg($value,$newid){
      print "<img id=\"$newid\" src=\"$value\"/>";
}

No need for other functions.

Sarfraz
True. Unless you're going to be doing a lot more than you are, there is no reason this should be this huge object. These three lines of code work just as well.
cmcculloh
+2  A: 
  1. As a rule of thumb, you should never embed HTML in executable code. You probably would be better off with a templating system.
  2. If you want to explicitly declare getter/setter methods, it makes sense to make the corresponding properties protected. Otherwise there's nothing to stop you or anyone else to skip the methods and read/write the properties directly.
  3. What if the value passed to setUrl() contains unsanitized input? Every dynamic value that's rendered should be fed to htmlentities() or such. Imagine:

    setUrl('http://somesite.org/dir/?param1=&lt;script>alert("I CAN HAS HACK");</script>");

mst
That is good to know, but that kills my whole plan(:hmm, I wanted to use user info from a database to load the page, with there pics and settings.I know there are alot of frameworks and sites that probably do it for you, but I really want to learn more before I use them.If I passed the user info from database via JSON then had javascript write the page would that be better?
Alex
@alex - No, Javascript will not help you here. Look up `htmlspecialchars()`.
Tobias Cohen
+1  A: 

In this particular case you have an Immutable object which means that you can remove setters and use the following version:

class MyImageTestClass{  
    private $_url;  
    private $_id;  

    function __construct($id, $url) {  
        $this->_id = $id;
        $this->_url = $url;
    }  

    function loadImg(){  
        return "<img id=\"" . $this->_id . "\" src=\"" . $this->_url . "\"/>";  
    }  
}  

$n = new MyImageTestClass("foo", "bar.jpg");  

echo $n->loadImg();
Fedyashev Nikita
+3  A: 

I would use "private" or "protected" or "public" instead of "var". Also, I'd set the defaults in the constructor method after I declared the properties as private etc above.

Personally, this is how I would do it, but It's been a year or two since I've done PHP...

class ImageBuilder{  
    private $url;    
    private $ID;    

    function __construct($url, $ID) {  
        $this->setUrl($url); 
        $this->setID($ID);   
    }  

    function setUrl($url){  
        if($url == NULL){
            $this->url = "nfl2.jpg";
        }else{
            $url = htmlentities($url, ENT_QUOTES);
            $this->url = $url;
        }
    }  

    function setID($ID){  
        if($ID == NULL){  
            $this->ID = "one";  
        }else{
            $this->ID = $ID;  
        }   
    }  

    function setImg($url, $ID){  
        $this->setUrl($url);
        $this->setIId($ID);
    }   

    function printImg(){  
        print "<img id=\"{$this->ID}\" src=\"{$this->url}\"/>";  
    }  
}  

$n = new ImageBuilder("peterson.jpg", "two");  

$n->printImg();

Also, if you wanted to use in your database loop:

//pretend this is coming out of your DB
$db_img = array(array("url" => "url.png", "id" => "1"), array("url" => "blah.gif", "id" => "2"));

$image = new ImageBuilder();

foreach($db_img as $img){
    $image->setImg($img["url"], $img["id"]);
    $image->printImg();        
} 
cmcculloh
THANK YOU!!!, that is what I was looking for.
Alex
Happy New Year to you and the Little one in the Picture, and to everybody who responded, muchas gracias, I learned more in ten minutes here than in the whole chapter in our book.
Alex
You're welcome! I didn't test the code so I'm not 100% sure there are no errors...
cmcculloh
Thank you, happy new year to you as well. Glad to have been such a help.
cmcculloh
Just fyi yes there is a var keyword, it was used in the old php 4 classes ie: `class myClass { var myFoo, myBar; }`. Variables declared with `var` are assumed to be public.
Rob
Ah, thank you. I wasn't aware...
cmcculloh
+1  A: 

What I meant is that you should use a templating system so as to separate the preparation of output data from its visualization.

$data = $myLibrary->getSomeData($arguments);
$t = new SomeTemplatingEngine;
$t->setData($data);
$t->setTemplate('someTemplate.html');
return $t->renderToHtml();

As opposed to:

$r = mysql_query(...)
while ($row = mysql_fetch_assoc($r))
{
  print "<tr>";
  print "<td>";
  print $row['someField'];
  ...
}

My favourite template engine is Dwoo, try it.

mst