8 Replies - 3856 Views - Last Post: 02 December 2011 - 08:23 PM Rate Topic: -----

#1 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 26 November 2011 - 03:02 AM

I have a VERY weird error

So whenever I refresh the page, the error changes. Here are a few of the results:

Fatal error: Cannot redeclare class db in on line 58



Fatal error: Cannot redeclare class db in Unknown on line 0


Fatal error: Cannot redeclare class db in fj on line 58


Fatal error: Cannot redeclare class db in on line 11


Fatal error: Cannot redeclare class db in ��� on line 58


I have one main class, which contains every other class so I can easily access any class from anywhere.

Here's main.php (the main class):
<?php
class main
{
	private $db, $web, $fj;
	
	public function __construct(){
		require "inc/db.class.php";
		require "inc/web.class.php";
		require "inc/fj.class.php";
		$this->db = new db();
		$this->web = new web();
		$this->fj = new fj();
	}
	
	public function web(){
		return $this->web;
	}
	
	public function fj(){
		return $this->fj;
	}
	
	public function db(){
		return $this->db;
	}
}
?>




here's the fj class:
<?php
class fj extends main
{
	/*
	 * updateTopImages(int)
	 * updates the top images and database until 50 pages. Starts on the id given. Recursive.
	 * returns true on success, false on fail
	*/
	public function updateTopImages($id){
		/* load up our variables */
		require "inc/parse.lib.php"; // Load our parse library
		$page = "http://www.funnyjunk.com/funny_pictures/pages/picturetop24h/asc/" . $id;
		$source = parent::web()->getSource($page);
		$dom = new DOMDocument();
		@$dom->loadHTML($source); // Silenced because of bad html markup
		$divs = $dom->getElementsByTagName('div');
		
		/* cycle through divs until main is found and get all pictures */
		foreach($divs as $div){
			if($div->hasAttribute("class")){
				if($div->getAttribute("class") == "main"){
					$pics = $div->getElementsByTagName("li");
				}
			}
		}
		
		/* cycle through our pictures and strip data */
		$final = array();
		foreach($pics as $index => $pic){
			$fjid = str_replace("/funny_pictures/", "", $href);
			$fjid = substr($href, 0,strpos($href, "/"));
			$this->updateImage($fjid);
		}
	}
	/*
	 * getImageInfo(int)
	 * Strips information from that image. Doesn't save or anything.
	 * returns array of info
	*/
	public function getImageInfo($id){
		require "inc/parse.lib.php";
		$page = "http://www.funnyjunk.com/funny_pictures/" . $id;
		$source = parent::web()->getSource($page);
		$dom = new DOMDocument();
		@$dom->loadHTML($source); // Silenced because of bad html markup
		$final = array();
		$final['title'] = $dom->find("title")->item(0)->nodeValue;
		die($final['title']);
	}
	
	/*
	 * updateImage(int, string())
	 * updates information on that image. Insert picture into database if picture not found.
	 * returns true on success, false on fail
	*/
	public function updateImage($id, $extra=array()){
		$mysqli = parent::db()->connect();
		if($stmt = $mysqli->prepare("SELECT * FROM pictures WHERE fj_id=?")){
			$stmt->bind_param("i", $id);
			$stmt->execute();
			$stmt->store_result();
			if($stmt->num_rows != 0){
				if($stmt = $mysqli->prepare("UPDATE pictures SET title=?, description=?, thumbs=? WHERE fj_id=? LIMIT 1")){
					$stmt->bind_param("sssi", $title, $description, $thumbs, $id);
					$stmt->execute();
					$stmt->store_result();
					if($stmt->affected_rows == 1){
						return true;
					} else {
						return false;
					}
				} else {
					die("Update image failed: " . $mysqli->error);
				}
			} else {
				if($stmt = $mysqli->prepare("INSERT INTO pictures(title, description, uploader, thumbs, picture)")){
					
				} else {
					die("Update image failed: " . $mysqli->error);
				}
			}
		} else {
			die("Update image failed: " . $mysqli->error);
		}
	}
	
}
?>

(this is still a work in progress)

The web class:
<?php
/*
 * web.class.php
 * Handles cURL stuff such as posting data or downloading sources
*/
class web
{

	/*
	 * getSource(string)
	 * returns string
	 * Gets the source of a page
	*/
	public function getSource($page){
		return $this->curl($page, array(CURLOPT_FRESH_CONNECT => true, CURLOPT_RETURNTRANSFER => true)); 	
	}
	
	/*
	 * postData(string, string[])
	 * returns string
	 * Sends post data to the page and return the source
	*/
	public function postData($page, $data){
		$fdata = "";
		foreach($data as $x => $y){
			if($fdata != ""){
				$fdata .= "&";
			}
			$fdata .= urlencode($x) . '=' . urlencode($y);
		}
		return $this->curl($page, array(CURLOPT_POST => true, CURLOPT_POSTFIELDS => $fdata, CURLOPT_FRESH_CONNECT => true, CURLOPT_RETURNTRANSFER => true));
	}
	
	/*
	 * curl(string, string[])
	 * returns string
	 * Performs a cURL operation on the page using the options and returns the result
	*/ 
	public function curl($page, $options){
		$c = curl_init();
		curl_setopt($c, CURLOPT_URL, $page); 
		curl_setopt_array($c, $options);
		$response = curl_exec ($c);
		curl_close ($c);
		return $response;
	}
}
?>



and the db class:
<?php
class db extends main
{
	public function connect(){
		return new mysqli("localhost", "root", "******", "fj");
	}
}
?>





No idea what's going on or why it's throwing this error. This has to be the weirdest thing to happen to me with PHP.

This post has been edited by creativecoding: 26 November 2011 - 03:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error: Cannot redeclare class db in Unknown on line 0

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 26 November 2011 - 04:05 AM

Looked at the log, there are some where the <?> symbols are marked down as:

\xac\xc6\x83

or

\x88\xfd\x8f
Was This Post Helpful? 0
  • +
  • -

#3 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 26 November 2011 - 04:12 AM

Hi,

I think the problem is this:

You have 1 parent class called "main", now when that is constructed it is including the classes of db, fj and web and creating objects of them.

However, the db and fj classes are an extension of "main", so when an object of db is created, it's running the same __construct() function as its parent (main) and therefore trying to create another db, fj and web object, so you are getting that error about trying to redeclare a class.

If you add in another __construct() function to your db.class and fj.class, e.g.

public function __construct()
{
}



And leave it empty, it will overwrite the parent construct and you won#t get that error any more.

This post has been edited by Duckington: 26 November 2011 - 05:30 AM

Was This Post Helpful? 1
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 26 November 2011 - 11:23 PM

Ah, thanks. I would have never caught that.
Was This Post Helpful? 0
  • +
  • -

#5 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 27 November 2011 - 03:35 AM

I cannot seem to access the other classes though. Line 46 ($source = parent::web()->getSource($page);) produces the error: Fatal error: Call to a member function getSource() on a non-object
Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 27 November 2011 - 10:52 AM

That's because you're calling the inherited web() function of your main class on your fj object. The fj object has a web property, which it inherited from main, and when calling parent::web() internally, it will see the fj object's local, never-declared web property, and thus come back as NULL, causing your getSource() call to be performed on a non-object. You will need to have it access the web property of your main object. Usually a good rule of thumb when working with objects you'll only need one of is to make them static, since you'll never need an actual instance of it, just access to it.

This post has been edited by Valek: 27 November 2011 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#7 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 27 November 2011 - 02:07 PM

Hi,

You need to declare your properties in main to be something other than private for a subclass to access them. protected would allow you to enforce encapsulation while allowing subclasses of main to see the properties.

Edit:

Also once you declare the properties in main to be protected you can access them using $this rather than parent as the properties will be inherited thus the web class has a $web property.

And and another edit :) I am willing to bet that "your cannot redeclare class" errors are a result of the class being included/required multiple times. Use require_once /include_once to prevent this from happening.

This post has been edited by Jstall: 27 November 2011 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#8 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 02 December 2011 - 08:09 PM

I'm getting a "Fatal error: Call to a member function getSource() on a non-object in (snip) on line 46"

Line 46 is:
$source = $this->web->getSource($page);


Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Fatal error: Cannot redeclare class db in Unknown on line 0

Posted 02 December 2011 - 08:23 PM

As a rule of thumb when working with OOP, the parent classes should know nothing about their children classes. This is the different between an is-a and a has-a relationship. That design is the root of your problems here. Rework your design and your problems should go away.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1