overriding functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
civax
Forum Newbie
Posts: 2
Joined: Sat Nov 08, 2003 3:49 pm

overriding functions

Post by civax »

Hi,

I've coded a script that scans sites based on profiles made by users and return results. Now, each profile has a function rewrite() that rewrites data according to the site.

I now try to scan multiple sites.
It all works well for the first site, then it throws Fatal error: Cannot redeclare rewrite() in ...

My question - How can I import each profile (php file) for temporary usage - if it is possible?
I'm also trying to think of alternative ways to have the rewrite function called. Or maybe a way to update the function content whenever a new profile is being called.

Any help?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

The error means that you are trying to include two or more functions with the same name into PHP.

Why not simply have the function in the main page without having to include it, and then in each profile just send any infomation to the the function.

Here's a basic example...

MAIN PHP PAGE

Code: Select all

<?php

function test($arg)
{
    echo $arg."<br />";
}

require_once("profile_one.php");
require_once("profile_two.php");

?>
PROFILE_ONE.PHP

Code: Select all

<?php
test("stuff from profile one");
?>
PROFILE_TWO.PHP

Code: Select all

<?php
test("stuff from profile two");
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Extending an abstract, base class is a common technique to overide functions but I don't know if that's what your're looking for.

In the child, you'd define a function with the same name as the overidden method in the parent class. The parent method simply contains a die('method not implemented'); line to make sure it DOES get overidden.

A good source for OOP techniques is phppatterns.com.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I thougt I could mention create_function() alsothat lets you create temporary functions based on runtime information (if wanted).

However, this might consume memory on occasion depending on how many new functions are created.
civax
Forum Newbie
Posts: 2
Joined: Sat Nov 08, 2003 3:49 pm

Post by civax »

actually, to give more details, I'm trying to extend this script that I've wrote:

http://cfxweb.net/civax/code.htm#newsgrabber

the profiles of each site contains few string vars and 1 function.

I've just rewroe the script to handle scanning of SEVERAL sites in one go. Basically what I'm trying to do is get the news grabbing in a loop and each time giving it a new profile. The problem is that with the second import of a profile file I get the mentioned error.

I don't mind changing the profiles format. I just need to have it all the profile details in 1 file to ease up on users creating new profiles for themselves.
Post Reply