0

When I visit this page: https://mysite.xyz/mymodule/sale I getting an error:

Fatal error: Uncaught Error: Call to undefined function get_special_key()

An action for https://mysite.xyz/mymodule/sale

public function executeSale() {
  $this->redirect('https://auth.sandbox.ebay.com/oauth2/authorize?client_id='.get_special_key('EBAY_CLIENT_ID').'&redirect_uri='.get_special_key('EBAY_REDIRECT_URI').'&response_type=code&scope='.self::EBAY_SCOPES);
  // Get the sale pictures
    $q = Doctrine_Query::create()
        ->select('sp.id, sp.pic_order')
        ->from('SalePicture sp')
        ->where('sp.sale_id = ?', 606223)
        ->orderBy('pic_order')
        ;
    $arSalePictures = $q->execute([], Doctrine::HYDRATE_ARRAY);

    echo 'SALE PICTURES: ';
    print_r($arSalePictures);
}

but, when I visit https://mysite.xyz/mymodule/sale while an acction is like this:

public function executeSale() {
  /*
  * Some
  * Code
  * ...
  */
  $this->redirect('https://auth.sandbox.ebay.com/oauth2/authorize?client_id='.get_special_key('EBAY_CLIENT_ID').'&redirect_uri='.get_special_key('EBAY_REDIRECT_URI').'&response_type=code&scope='.self::EBAY_SCOPES);
}

everything works as expected.

The get_special_key() function is autoloaded by symfony.

Any suggestions would be helpful, thank you!

Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35
  • 2
    Doesn't sound like it's being loaded. What / where is it? Not sure how everything is working as expected with a fatal error... – Jonnix Mar 19 '19 at 11:13
  • @Jonnix but action does work when I call that function at the bottom of an action. – Salim Ibrohimi Mar 19 '19 at 11:15
  • So what happens in the `some code` section? Something appears to load up the file with your function. – Jonnix Mar 19 '19 at 11:16
  • @YvesLeBorg what do you mean? – Salim Ibrohimi Mar 19 '19 at 11:16
  • @Jonnix `some code` section just connects to database and select some info. – Salim Ibrohimi Mar 19 '19 at 11:17
  • 2
    I mean that what is relevant here is what happens in **some code**. – YvesLeBorg Mar 19 '19 at 11:17
  • @YvesLeBorg do you want to see what in the `some section` block? The code was edited. – Salim Ibrohimi Mar 19 '19 at 11:21
  • 1
    So can you : it is probably when any one of the statements with Doctrine statics that creates an autoload which eventually defines your missing function. Kick the tires. Maybe put a line `$dummy = Doctrine::HYDRATE_ARRAY;` before the query. If that does not work, it is the query that cause the load. assuming that **some code** is the same as what you showed. Again **that** is the most relevant piece (not shown, assumed same as above) – YvesLeBorg Mar 19 '19 at 11:27
  • @YvesLeBorg okay, thank you! :) – Salim Ibrohimi Mar 19 '19 at 11:32
  • If the hack i suggested worked, and you chose to keep it (as in not fix the situation), you should also put a FAT COMMENT as a warning to maintainers that a useless looking line of code is actually doing smnthng fundamental. Show love for the maintainers that will deal with your code long after you are gone from the project :) – YvesLeBorg Mar 19 '19 at 11:34
  • @YvesLeBorg You were right `some code` caused get_special_key() functions loading. But I don't understand why. – Salim Ibrohimi Mar 19 '19 at 12:03

1 Answers1

0

Adding this answer here because of the generic use of "function name" in the error message. This seems like the best place for the answer, given that the question text would essentially be the same. Switching from Java to PHP I wasn't aware that when calling a function within a class the use of the reference to the current object (this) is mandatory, i.e. you will get an undefined function error if you do not call the function as follows:

$this->insert_function_name_here($parameter);

I actually guessed at the use of $this - I couldn't find a reference to this in the PHP manual. Added something to Reference — What does this symbol mean in PHP?. W3 Schools has something - https://www.w3schools.com/php/php_oop_classes_objects.asp.

B5A7
  • 863
  • 12
  • 20