logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 03-31-2008, 02:31 AM   #1
smitho
 
Join Date: Dec 2007
Posts: 149
Default First attempt at class in php

Spent the last couple of weeks learning OOP and understand the tutorials and any example I see but when you first try and apply it to something you are doing it looks very different.

I've got a process where folders on the web server are read using this code (want to keep it simple) which is part of a larger process.

PHP Code:
if ($handle opendir('./images')) {
    echo 
"Directory handle: $handle<br>";
    echo 
"Files:<br><br>";

    while (
false !== ($file readdir($handle))) {
        if(
$file != "." && $file != ".." && $file != ".DS_Store"){
        echo 
"$file<br>";
        }
    }
    
closedir($handle);

Now to use this as a class I've coded this:

PHP Code:
class Folder {

    var 
$handle;
    var 
$file;
    
    function 
__construct($location){
    
    
$this->handle $location;
    
    }
    
    
    function 
read_folder($location){
    
        
$this->handle opendir($location);
        echo 
"Directory handle:".$this->handle."<br>";
        echo 
"Files:<br><br>";
        while (
false !== ($this->file readdir($this->handle))) {
        if(
$this->file != "." && $this->file != ".." && $this->file != ".DS_Store"){
        echo 
$this->file."<br>";
        }
      }
    }


The page that calls the class functions looks like this:

PHP Code:
include("FolderClass.php"); 


$images1 = new Folder("./images");

$images1->read_folder("./images"); 
Am I coding this correctly? It works but I just want to make sure I'm getting it correct.
smitho is offline   Reply With Quote
Old 04-01-2008, 08:23 AM   #2
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

it looks fine to me
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 04-08-2008, 09:57 PM   #3
pete
 
Join Date: Apr 2008
Location: in a bin
Posts: 113
Default

You shouldn't be echo'ing out from a method of a class (when talking classes, what you guys refer to as functions should be called methods ).

You should be returning a string of HTML in your example then use

PHP Code:
echo $images1->read_folder("./images"); 
You would also usually create set/get methods but you have the general idea, just keep on practicing and perhaps get some OOP PHP books.
pete is offline   Reply With Quote
Old 04-09-2008, 12:38 AM   #4
smitho
 
Join Date: Dec 2007
Posts: 149
Default

Thanks Pete,

You are correct in all the tuts I've gone through it is not a good idea to echo in a class method. If I take out the echo from the class nothing happens. If i change echo to return I only get the first line:

Directory handle:Resource id #5

Any suggestions?
smitho is offline   Reply With Quote
Old 04-09-2008, 07:23 AM   #5
pete
 
Join Date: Apr 2008
Location: in a bin
Posts: 113
Default

In your example for method read_folder you would do this:
PHP Code:
function read_folder($location){
    
        
$this->handle opendir($location);
        
$sOutput "Directory handle:".$this->handle."<br>";
        
$sOutput .= "Files:<br><br>";
        while (
false !== ($this->file readdir($this->handle))) {
        if(
$this->file != "." && $this->file != ".." && $this->file != ".DS_Store"){
        
$sOutput .= $this->file."<br>";
        }
      }
      return 
$sOutput;
    } 
If you look at example 1 on this page http://uk3.php.net/function.opendir you will see you should be checking to see whether the location is actually a directory and then also check whether the handle is a valid resource. So
PHP Code:
if(is_dir($location)){
   if(
$this->handle opendir($location)){
.... 
So the returned value is either a string (list of files) or false, so I would add another method to determine the output, such as $images1->isValid() which would be a boolean and make it easier to ensure the value returned.

You would also perhaps want to return only an array of files from your class method, the HTML should never be part of the class, add any HTML outside the class in the PHP page.
pete is offline   Reply With Quote
Old 04-09-2008, 12:03 PM   #6
smitho
 
Join Date: Dec 2007
Posts: 149
Default

Greatly appreciated pete,

I mist the if ($handle = opendir('./images')) { from the original example. Iíll give this a go.

Cheers.
smitho is offline   Reply With Quote
Old 04-17-2008, 12:48 PM   #7
smitho
 
Join Date: Dec 2007
Posts: 149
Default

Finally finished the other code I've been working on and I can focus on this again.

I've followed pete's advice but its not returning anything don' t know what I'm missing:

PHP Code:
class Folder {

    var 
$handle;
    var 
$file;
    
    function 
__construct($location){
    
    
$this->handle $location;
    
    }
    
    
    function 
read_folder($location){
    
        if(
is_dir($location)){
        
$this->handle opendir($location);
          
$sOutput "Directory handle:".$this->handle."<br>";
        
$sOutput .= "Files:<br><br>";
        while (
false !== ($this->file readdir($this->handle))) {
        if(
$this->file != "." && $this->file != ".." && $this->file != ".DS_Store"){
        
$sOutput .= $this->file."<br>";
        }
      }
      return 
$sOutput;
    }
    }  
    

PHP Code:
include("FolderClass.php");

$images1 = new Folder("./images");

$images1->read_folder("./images"); 
smitho is offline   Reply With Quote
Old 04-17-2008, 12:58 PM   #8
pete
 
Join Date: Apr 2008
Location: in a bin
Posts: 113
Default

Try this:

PHP Code:
<?php
class Folder {

    var 
$handle;
    var 
$file;
    
    function 
__construct($location){
        
$this->location $location;
    }
    
    
    function 
read_folder(){
        if(
is_dir($this->location)){
        
$this->handle opendir($this->location);
          
$sOutput "Directory handle:".$this->location."<br>";
        
$sOutput .= "Files:<br><br>";
        while (
false !== ($this->file readdir($this->handle))) {
        if(
$this->file != "." && $this->file != ".." && $this->file != ".DS_Store"){
        
$sOutput .= $this->file."<br>";
        }
      }
      return 
$sOutput;
    }
    }  
    
}
$oImages = new Folder('images/');
echo 
$oImages->read_folder();
?>
pete is offline   Reply With Quote
Old 04-17-2008, 01:31 PM   #9
smitho
 
Join Date: Dec 2007
Posts: 149
Default

Thanks pete,

just went over davidj's class example and saw the echo $results.

So this works fine.

PHP Code:
include("FolderClass.php");

$images1 = new Folder("./images");

$result $images1->read_folder("./images"); 
HTML Code:
<body>

<?php  


echo $result; 


?>

</body>
Cheers.
smitho is offline   Reply With Quote
Old 04-18-2008, 11:59 AM   #10
smitho
 
Join Date: Dec 2007
Posts: 149
Default

This is not as easy as I thought the next step in the process is to take the files in the folder (in this case pdfs) and create a jpg preview of each one.

The code I use normally is :

PHP Code:
$justname substr($file0, -4);

    
$in="images/$file";
    
$out="preview/$justname.jpg";
    
$result exec("/wamp/www/gs/gswin32c.exe -q -dNOPAUSE -dBATCH -sDEVICE=jpeg -sOutputFile=$out $in 2>&1"); 
Any suggestions on how to combine $images1->read_folder("./images"); to create the preivews would be a great help.
smitho is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:49 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com