Php unexpected T_String on php4 webhost but not on php5 localhost
I am getting a really frustrating problem with my php script below. Everything works as expected on my local machine using XAMPP, but when I put it on the web I get:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/38/d220091779/htdocs/roughgiraffed/RG2/portfolio_item_vars.php on line 20
I would greatly appreciate it if someone would take a quick look for me, or offer some advice.
The error is on the line directly below array_push($projectImages, $filePath);
near the bottom of the function.
EDIT: Even though the error is listed as being o开发者_StackOverflown line 20 (in getImages()) I removed the class below and the error went away. Of course, I still want the class to work... but does this mean it is a problem with my php version after all?
<?php
$pageTitle = "Portfolio";
//return an array of the (non-featured, non-thumb) images within a given project
function getImages($category, $project){
$projectImages = Array();
if ($dir = opendir("portfolio/" . $category . "/" . $project . "/")){
//open each 'file' in the directory
while($file = readdir($dir)){
$filePath = "portfolio/" . $category.'/'.$project.'/'.$file;
//check if it really is a file
if($file != "." && $file != ".." && $file[0] != '.' && is_file($filePath)) {
//check if it is an image
if(getimagesize($filePath)){
//ignore featured_image and _thumbs
if(!strstr($file, "featured_image") && !strstr($file, "_thumb")){
//Add the non-featured, non-thumb image to the array
array_push($projectImages, $filePath);
}
}
}
}
closedir($dir);
}
return $projectImages;
}
class ImgResizer {
private $originalFile = '';
public function __construct($originalFile = '') {
$this -> originalFile = $originalFile;
}
public function resize($newWidth, $targetFile) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious
}
}
?>
My Localhost is running PHP Version 5.3.1
The WebHost is running PHP Version 4.4.9
This weird kind of parse errors may happen in PHP4 if you use PHP5 specific things, like the public
keyword, which was introduced with PHP5.
You maybe should have a look at the official PHP5 backward incompatibilties list when planning to run the very same source code under PHP4 and PHP5.
Excerpt:
Backward Incompatible Changes
Although most existing PHP 4 code should work without changes, you should pay attention to the following backward incompatible changes:
- There are some new reserved keywords.
- strrpos() and strripos() now use the entire string as a needle.
- Illegal use of string offsets causes E_ERROR instead of E_WARNING.
:
You can't use __construct() in PHP 4.
You have to use a function named the same as the class name.
Use ImgResizer() in place of __construct(), or get a better web host. PHP 4 is very old.
Thanks to the help of those in the comments, I determined that it was, in fact, an issue with php versions. Still, I have no idea why it was giving the error at line 20, about ten lines above the offending code.
精彩评论