PHP object property declaration error
I'm having a bit of trouble defining a property in a PHP class I'm creating.
<?php
class news_parser {
public $var1 = Array();
function contents($parser, $data) {
开发者_如何学编程 printf($data);
}
function start_tag($parser, $data, $attribs) {
printf($data);
}
function end_tag($parser, $data) {
printf($data);
}
function parse() {
if(!$file = fopen("http://services.digg.com/2.0/story.getTopNews?type=rss&topic=technology", "r"))
die("Error opening file");
$data = fread($file, 80000);
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, array($this, "start_tag"), array($this, "end_tag"));
xml_set_character_data_handler($xml_parser, array($this, "contents"));
if(!xml_parse($xml_parser, $data, feof($fh)))
die("Error on line " . xml_get_current_line_number($xml_parser));
xml_parser_free($xml_parser);
fclose($fh);
}
}
$digg_parser = new news_parser();
$digg_parser->parse();
echo phpversion();
?>
Produces the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /homepages/8/d335242830/htdocs/caseyflynn/php/display_formatted_RSS_feed.php on line 3
As far as I can tell I have the correct syntax. My server is running PHP 4.5. Any ideas?
My server is running PHP 4.5
This is your problem: PHP 4 doesn't know the public
keyword - along with a heap of other OOP features.
As @konforce says, in the code you show, you can simply switch to using the var
keyword. But the best thing would really be to switch to PHP 5. PHP 4's time is really, really over.
精彩评论