Jump to content

Archived

This topic is now archived and is closed to further replies.

boarderstu

OOPHP

Recommended Posts

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.
[code]
<?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();[/code]

[code]
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[/code]

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

thanks in advance, im going to work. :)

bye guys!

Share this post


Link to post
Share on other sites
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--]

Share this post


Link to post
Share on other sites
[!--quoteo(post=371638:date=May 5 2006, 02:33 PM:name=boarderstu)--][div class=\'quotetop\']QUOTE(boarderstu @ May 5 2006, 02:33 PM) [snapback]371638[/snapback][/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.
[code]
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

[/code]

[/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:
[code]
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

[/code]

EDIT BY OBER: fixed your code block

Share this post


Link to post
Share on other sites
[!--quoteo(post=371644:date=May 5 2006, 07:44 PM:name=lead2gold)--][div class=\'quotetop\']QUOTE(lead2gold @ May 5 2006, 07:44 PM) [snapback]371644[/snapback][/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:
[code]
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

[/code]

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.

Share this post


Link to post
Share on other sites
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

[code]
<?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();
?>
[/code]



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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=371940:date=May 7 2006, 12:25 AM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ May 7 2006, 12:25 AM) [snapback]371940[/snapback][/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

Share this post


Link to post
Share on other sites
[!--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 [i]jeremywesselman[/i] 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().

Share this post


Link to post
Share on other sites


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

and now have the revised code to:

[code]<?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();

?>[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]
function __construct($thumbdir,$imagedir,$image,$start,$end = $start+$max)
        {
        $this->thumbdir = $thumbdir;
        $this->imagedir = $imagedir;
        $this->image = $image;
        $this->MAX = $end;        
}// end construct

[/code]

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.

Share this post


Link to post
Share on other sites
As yonta has already stated, your constructor will be named after your class with PHP4. Also, you will use [b]var[/b] to declare your variables instead of [b]public[/b] or [b]private[/b].

[code]<?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();

?>[/code]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.