Debug help

Discussion in 'PHP' started by plussy, Aug 13, 2012.

  1. #1
    Ok here is my problem

    I have a class A and a class B that extends class A.

    Both classes have a function called create.

    the create function from class A looks like this

    
    function create &create( $service, $component, $tariff) {
    
    }
    
    PHP:
    the create function from class B looks like this

    
    function create( $service, $component, $tariff, $date, $handle, $start) {
    
    }
    
    PHP:
    In my error_log I see this entry

    Any idea what the problem could be?
     
    plussy, Aug 13, 2012 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    I can't replicate it with:

    <?php
    
    error_reporting(E_ALL);
    
    
    class A {
        function &create( $service, $component, $tariff) {
            echo 123;
        }
    }
    
    
    class B Extends A {
        function create( $service, $component, $tariff, $date, $handle, $start) {
            echo 456;
        }
    }
    
    
    $b = new B;
    $b->create('test', 'test2', 'test3', 'test4', 'test5', 'test6'); 
    Code (markup):
    Would you be able to paste the code you're actually using?

    Either way, if you make your error_reporting PHP configuration setting less strict​, it should hide the error.
     
    Alex Roxon, Aug 13, 2012 IP
  3. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #3
    sorry can't really give any more details. But I think the problem is that one function has the & and the other doesn't. Any idea what that means?
     
    plussy, Aug 13, 2012 IP
  4. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #4
    can you expand on why do you think that?
     
    plussy, Aug 13, 2012 IP
  5. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    I'm not quite sure you should have that & infront of "create". & is usually used when you pass a variable by reference, and I've never seen it used infront of a function name like that.
    The error you're getting is due to the fact that you're redeclearing the function "create" with other parameters in class B than the original method in class A.
    You need to use the exact same declaration in a subclass in PHP, and you cannot do actual method overloads like in java/C# etc (where you define multiple functions with different arguments).
     
    Soulstone, Aug 13, 2012 IP
  6. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #6
    From memory, prefixing a function name with an ampersand simply means it returns a reference to a variable, rather than a value.
     
    Alex Roxon, Aug 14, 2012 IP
  7. Soulstone

    Soulstone Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    That makes sense. The problem is still that the method declaration has to be equal to the one in class A
     
    Soulstone, Aug 15, 2012 IP
  8. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #8
    It shouldn't though - PHP shouldn't be that strict. I even tested on PHP 5.3.13 with the error reporting level set to E_ALL, and no error was raised. The child class should be able to override the parent class with a different set of parameters. It'd be interesting to see OPs php.ini file.

    Either way, it's more a warning, and it should be suppressed on a production environment.
     
    Alex Roxon, Aug 15, 2012 IP