Jump to content


Photo

OOPHP


  • Please log in to reply
11 replies to this topic

#1 boarderstu

boarderstu
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 05 May 2006 - 06:33 PM

right so i have a gallerly that i created in PHP, and have recently studied OOP in JAVA at university, so i decited to convert all my PHP gallery code to OO to make it easier to handle.

this is not as easy as it seems.

i started small with the following code and i get the follow error.
<?PHP  class Gallery  
{

        private $thumbdir;
        private $imagedir;
        private $image;
        private $MAX;
        private $displayStart;
        private $displayEnd;

function __construct($thumbdir,$imagedir,$image)
        {
        $this->thumbdir = "photos/".$_GET['folder']."thumbs/";
        $this->imagedir = "photos/".$_GET['folder'];
        $this->image = $imagedir.$_GET['image'];
        $this->MAX = "200";
        $this->displayStart = $_GET['start'];
        $this->displayEnd = "200";
        }// end construct
function display()
    {
        return $this->thumbdir;
    
    }
    
} //end the class



$gal = new Gallery;
$gal->display();

Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /var/www/html/stu/Gallery/photos-oo.php on line 17

anybody got any advice to offer or some good OOPHP sites?

thanks in advance, im going to work. :)

bye guys!

#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 05 May 2006 - 06:40 PM

What version of PHP are you using? Because PHP4 OOP and PHP5 OOP have some syntax differences.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]

#3 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 05 May 2006 - 06:44 PM

[!--quoteo(post=371638:date=May 5 2006, 02:33 PM:name=boarderstu)--][div class=\'quotetop\']QUOTE(boarderstu @ May 5 2006, 02:33 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
right so i have a gallerly that i created in PHP, and have recently studied OOP in JAVA at university, so i decited to convert all my PHP gallery code to OO to make it easier to handle.

this is not as easy as it seems.

i started small with the following code and i get the follow error.
function __construct($thumbdir,$imagedir,$image)
        {
        $this->thumbdir = "photos/".$_GET['folder']."thumbs/";
        $this->imagedir = "photos/".$_GET['folder'];
        $this->image = $imagedir.$_GET['image'];
        $this->MAX = "200";
        $this->displayStart = $_GET['start'];
        $this->displayEnd = "200";
        }// end construct


[/quote]

I don't know for sure, but why don't you use the inputs to the constructor here instead of reading from $_GET ?


ie:
function __construct($thumbdir,$imagedir,$image,$start,$end = 200)
        {
        $this->thumbdir = $thumbdir;
        $this->imagedir = $imagedir;
        $this->image = $image;
        $this->MAX = $end;
        $this->displayStart = $start;
        $this->displayEnd = $end;
        }// end construct


EDIT BY OBER: fixed your code block

#4 boarderstu

boarderstu
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 06 May 2006 - 12:21 PM

[!--quoteo(post=371644:date=May 5 2006, 07:44 PM:name=lead2gold)--][div class=\'quotetop\']QUOTE(lead2gold @ May 5 2006, 07:44 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I don't know for sure, but why don't you use the inputs to the constructor here instead of reading from $_GET ?
ie:
function __construct($thumbdir,$imagedir,$image,$start,$end = 200)
        {
        $this->thumbdir = $thumbdir;
        $this->imagedir = $imagedir;
        $this->image = $image;
        $this->MAX = $end;
        $this->displayStart = $start;
        $this->displayEnd = $end;
        }// end construct


EDIT BY OBER: fixed your code block
[/quote]


no im afraid the error is still there. there error occurs when i declare my private variables.

#5 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 07 May 2006 - 04:19 AM

you have a few errors or 'i would do it differently' situations

first you dont provide the arguments when you call the class.

$gal = new Gallery;

to

$gal = new Gallery($arg, $arg2 etc..);

those arg's match whatever you declaire in the construct


next i'd take the $_GET outside of the class itself and put it in the $gal = new Gallery($_GET...


third you can set the properties that are constant before the constructor. so things like $max you can take out and put it here

private $max = 200; etc

<?PHP  class Gallery  
{

        private $thumbdir;
        private $imagedir;
        private $image;
        private $MAX;
        private $displayStart;
        private $displayEnd;

function __construct($thumbdir,$imagedir,$image,$start)
        {
        $this->thumbdir = "photos/".$thumbdir."thumbs/";
        $this->imagedir = "photos/".$imagedir;
        $this->image = $imagedir;
        $this->displayStart = $start;
        }// end construct
function display()
    {
        return $this->thumbdir;
    
    }
    
} //end the class


//set vars to be passed in class
$thumbdir = $_GET['folder'];
$imagedir = $_GET['folder'];
$image = $_GET['image'];
$start = "";

$gal = new Gallery($thumbdir,$imagedir,$image,$start);
$gal->display();
?>



after looking at your code somemore, i feel that you think that you may have to call a function, define its vars and use $_GET[var] to use that var in the function. am i right about that? if so, you only need to do
function name($var){
$var;
}
to use it. you use $_GET when you're pulling something from a url

#6 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 07 May 2006 - 04:25 AM

When you're writing classes, you can't have any code outside of functions - you should put those few lines of code above your __construct method into your __construct method.
http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#7 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 07 May 2006 - 01:55 PM

[!--quoteo(post=371940:date=May 7 2006, 12:25 AM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ May 7 2006, 12:25 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When you're writing classes, you can't have any code outside of functions - you should put those few lines of code above your __construct method into your __construct method.
[/quote]
he is setting up the class' properties. thats legal, actually i think its required in php5

#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 May 2006 - 01:57 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]When you're writing classes, you can't have any code outside of functions[/quote]
What? You can declare properties outside of methods.

As jeremywesselman already asked.... What version of php are you running? There are quite a few differences in the way php4 and php5 handle OO. Your code looks to be coded for php5 and will cause all sorts of errors in php4.

But yeah, as has been said. Your not passing any arguments to your __construct().

#9 boarderstu

boarderstu
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 08 May 2006 - 01:08 PM



im using PHP 4.3.3 on fedora core 3 or 4 (i think)

and now have the revised code to:

<?PHP
         $thumbdir = "photos/".$_GET['folder']."thumbs/";
        $imagedir  = "photos/".$_GET['folder'];
        $image  = $imagedir.$_GET['image'];
        $MAX = 200;
        $Start = $_GET['start'];



class Gallery
{

        private $thumbdir = "photos/".$_GET['folder']."thumbs/";
        private $imagedir  = "photos/".$_GET['folder'];
        private $image  = $imagedir.$_GET['image'];
        private $MAX = 200;
        private $start = $_GET['start'];
        private $end;


function __construct($thumbdir,$imagedir,$image,$start,$end = $start+$max)
        {
        $this->thumbdir = $thumbdir;
        $this->imagedir = $imagedir;
        $this->image = $image;
        $this->MAX = $end;
        $this->displayStart = $start;
        $this->displayEnd = $end;
        }// end construct
function display()
    {
        return $this->thumbdir;

    }

} //end the class



$gal = new Gallery($thumbdir,$imagedir,$image,$start,$MAX);
$gal->display();

?>

however this code is for php5 as im using a Core PHP Programming (revised for pHP5).
how would i go about changing this to php4?

many thanks

#10 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 08 May 2006 - 02:41 PM

Read up on objects in [a href=\"http://au.php.net/oop\" target=\"_blank\"]php4[/a], though may I suggest you upgrade. Especially seeing as your reading a php5 book. php4 is much more limited in its OOP capabilities.

#11 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 08 May 2006 - 03:15 PM

function __construct($thumbdir,$imagedir,$image,$start,$end = $start+$max)
        {
        $this->thumbdir = $thumbdir;
        $this->imagedir = $imagedir;
        $this->image = $image;
        $this->MAX = $end;        
}// end construct


I think that in php4 your constructor should be named the same as the class (function Gallery) and not _construct since that's a php5 syntax.

You can check the [a href=\"http://us2.php.net/oop\" target=\"_blank\"]manual [/a]for classes in php4.

do it, do it right, do it right now

#12 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 08 May 2006 - 08:48 PM

As yonta has already stated, your constructor will be named after your class with PHP4. Also, you will use var to declare your variables instead of public or private.

<?php
class Gallery
{
        var $thumbdir = "photos/".$_GET['folder']."thumbs/";
        var $imagedir  = "photos/".$_GET['folder'];
        var  $image  = $imagedir.$_GET['image'];
        var $MAX = 200;
        var $start = $_GET['start'];
        var $end;


function Gallery($thumbdir,$imagedir,$image,$start,$end = $start+$max)
        {
        $this->thumbdir = $thumbdir;
        $this->imagedir = $imagedir;
        $this->image = $image;
        $this->MAX = $end;
        $this->displayStart = $start;
        $this->displayEnd = $end;
        }// end construct

function display()
    {
        return $this->thumbdir;
     }

} //end the class



$gal = new Gallery($thumbdir,$imagedir,$image,$start,$MAX);
$gal->display();

?>

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users