TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-05-2008, 06:54 PM   #1 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default Redeclaring class, but i don't ?

Well i keep getting this error:

Fatal error: Cannot redeclare class clean in /customers/pulse101.net/pulse101.net/httpd.www/dev1/backend/classes/clean.class.php on line 66

But i just can't understand it :(

Here is the file with the "error"?
PHP Code:
<?php
//Check if ticket
if (!defined('BACKEND_TICKET'))
{
    die();
}

//Declare the class
class clean extends p101 
{
    
    public function 
clean_var($var
    {
        
//If $var = array , treat as an array
        
if (is_array($var)) 
        {
            
//$buffer = array();
            
foreach ($var as $key => $val
            {
                if (
is_array($var[$key])) 
                {
                    
$var[$key] = $this->clean_var($val);
                } else 
                {
                    
$var[$key] = $this->do_clean($val);
                }
            }
        } else 
        {
            
$var $this->do_clean($var);

        }

        return(
$var);
    }
    
    public function 
clean_globals() 
    {
        
        
$array = array(
                            
'POST' => $_POST,
                            
'GET' => $_GET,
                            
'COOKIE' => $_COOKIE,
                            
'REQUEST' => $_REQUEST,
                            
'SESSION' => $_SESSION,
                            
'FILES' => $_FILES
                            
);
                            
        foreach(
$array as $key => $val
        {
            
$this->global_vars[$key] = $this->clean_var($val);
        }
    }
    
    private function 
do_clean($var
    {
        if(
function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc())
        {
            
$var stripslashes($var);
        }
        
$var addslashes($var);
        
$var htmlspecialchars($var);
        return(
$var);
    }
    
// Line 66

?>
If you wan't the class i'm extending then here it is, i got a little help from Kalle;
PHP Code:
<?php
//Check if ticket
if (!defined('BACKEND_TICKET'))
{
    die();
}

class 
p101 
{
    
    protected 
$instances = array();
    
    public function 
__construct() 
    {
        
        
// Load classes
        
        
foreach(glob(CLASS_PATH '*.class.php') as $filename
        {
            if (!
is_readable($filename)) 
            {
                
                
$this->error[] = 'File: ' $filename ' is not readable';
                continue;
            }
                require(
$filename);
                
                
//$classname = substr($filename, strlen(CLASS_PATH), strpos($filename, '.', 0));
                
$classname explode('/'$filename);
                
$classname end($classname);
                
$classname explode('.'$classname);
                
$classname $classname[0];

                if (
class_exists($classname)) 
                {
                    
$this->instances[$classname] = new $classname;
                } else 
                {
                    
$this->error[] = 'Problem declaring ' $classname;
                }
        }
        
        
        
// Clean globals
        
$this->clean->clean_globals();

    }
    
    public function 
__get($varname
    {
        
$varname = (string) $varname;

        if(
array_key_exists($varname$this->instances))
        {
            return(
$this->instances[$varname]);
        }
        elseif(isset(
$this->{$varname}))
        {
            return(
$this->{$varname});
        }
    }
}

?>
I'm pretty stuck with this at the moment. Any suggestions?
SpYkE112 is offline  
Reply With Quote
Old 06-05-2008, 07:18 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

My guess is that you're including the file that contains the clean class twice.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-05-2008, 07:22 PM   #3 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

But i don't the only place it gets included is in the __constructer :/
This is really annoying ^_^
SpYkE112 is offline  
Reply With Quote
Old 06-05-2008, 07:27 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I don't know about other people, but I once accidentally tried instantiating an extension of a class from within the class that it was extending, and it would kill apache everytime. I don't think you're supposed to be allowed to do that. (To break it down, it looks like even though clean extends p101, you're calling clean from within p101)
-m
delayedinsanity is offline  
Reply With Quote
Old 06-05-2008, 07:33 PM   #5 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

I'm pretty new to the whole OOP thing, so could you break it down so that a 3 year old would understand it??
SpYkE112 is offline  
Reply With Quote
Old 06-05-2008, 07:58 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Well my first question would be, do you really need to extend p101? It looks to me like they're two completely different beasts, ie, p101() intializes your classes and adds objects to a library, whereas clean() is meant for sanitizing strings/variables. When you extend a class in Object Oriented Programming it's generally so that the child class can use some method or property of the parent/super class in conjunction with new methods that it defines. If the two classes don't use any similar properties or methods, and in this case, don't even really do the same thing, there shouldn't be a need to create the child->parent relationship of an extended class.

Um, and I don't want to insult your intelligence because I'm sure you can keep up, but broken down for a three year old, I guess I would say something along the lines of.... well, spyke, that's a question you should be asking your mom.
-m
delayedinsanity is offline  
Reply With Quote
Old 06-05-2008, 08:05 PM   #7 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

Well, the plan is to start creating somekind of Framework thingy, where i wan't to use the class p101 as an umbrella, from where i can call every other class, and then i wan't it to be like, you class p101, class A and class B. Then i wan't it to be able to use functions from A in B it the plan is that everthing gets "called" through class p101..

It is still very much work in progress as i'm very fresh in the entire OOP world :)

I hope you understand my little plan ^_^
SpYkE112 is offline  
Reply With Quote
Old 06-05-2008, 08:29 PM   #8 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Kind of a chained singleton. You still don't need to extend anything at this point though, since you'll be able to call clean a la

PHP Code:
$pUmbrella = new p101();
$pUmbrella->clean->clean_var($szString); 
You may want to consider if you need to include and instantiate EVERY object on every page, every time. It seems like I'm doing this a lot lately, I feel kind of like a registryvangelist, but check this noise out: Registry pattern and definitely The Singleton Design Pattern for PHP <-- the singleton and registry are two of the easiest OOP design patterns to implement, but that's not to say they lack any power, as you'll find scripts all the way up to the Zend Framework using them.

If you want a great book on PHP object orientation and design patterns, take a peek at:
APRESS.COM : PHP Objects, Patterns, and Practice, Second Edition : 9781590599099

That book and Pro PHP are snazzy.
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
SpYkE112 (06-06-2008)
Old 06-06-2008, 05:44 AM   #9 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

Well, i looked at you links and decided to do it my own way, to ensure readabilty to me, and so that i know it works :)

Last edited by SpYkE112 : 06-06-2008 at 04:31 PM.
SpYkE112 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:20 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design