php, this array works in global but failed while putting inside class

223

I have following array, this works fine If i put in global and access the php with CLI but it gives following error while putting this same array in class.

'printer_1' => array(
    't_char_set' => 'ch-latin-2',

    'find' => array('Ä',      'Ö',      'Ü',      'ä',      'ö',      'ü' ,
                    'à',      'â',      'ç',      'é',      'è',      'ê' ,
                    'ë',      'ï',      'î',      'ì',      'ô',      'ò'),

    'repl' => array(chr(142), chr(153), chr(154), chr(132), chr(148), chr(129),
                    chr(133), chr(131), chr(135), chr(130), chr(138), chr(136),
                    chr(137), chr(139), chr(140), chr(141), chr(147), chr(149))
);                                  

while putting this as a variable in class it's gives following error

PHP Parse error: syntax error, unexpected '(', expecting ')' in xxx.php on line 6

any clue ?

edit: Here is the full class (I have removed the other functions to minimize the content.)

<?php
  $K_printer = array(


     'TM1' => array(

        /* Printer Setup */

             'width' => 40,
         'translate' => true,
        'p_char_set' => false,

        /* Printer Capabilities */

             'color' => false,
           'barcode' => true,
               'cut' => true,
              'logo' => true,
            'drawer' => true,
        'fontselect' => true,
           'reverse' => false,


        /* Character Translation Setup */

        't_char_set' => 'ch-latin-2',

              'find' => array('Ä',      'Ö',      'Ü',      'ä',      'ö',      'ü' ,
                              'à',      'â',      'ç',      'é',      'è',      'ê' ,
                              'ë',      'ï',      'î',      'ì',      'ô',      'ò'),

              'repl' => array(chr(142), chr(153), chr(154), chr(132), chr(148), chr(129),
                              chr(133), chr(131), chr(135), chr(130), chr(138), chr(136),
                              chr(137), chr(139), chr(140), chr(141), chr(147), chr(149))
     )
  );

  // KByte
  // =====

  if(!function_exists('kchr')){
     function kbyte($val){
        return pack('C', $val);
     }
  }


  class KP{


       private $width;                 // output width (characters)
       private $translate;             // use character translation
       private $printer_char_set;      // printer character set

     // Constructor
     // ===========

     function __construct($printer_setup = false){
        // Load Printer Setup
        // ==================

        $this -> LoadSetup($printer_setup);
     }

     //removed other functions...

     //
     // Load Printer Setup
     // ==================

     function LoadSetup($printer = false){
        global $K_printer; 

        if(!isset($K_printer[$printer])){
           $this -> error('Unknown printer setup.');
           return false;
        }

        $this -> width = $K_printer[$printer]['width'];
        $this -> translate = $K_printer[$printer]['translate'];
        $this -> printer_char_set = $K_printer[$printer]['p_char_set'];
        return true;
     }


  } 

?>

This works fine from PHP cli with following say t1.php file

      $ep = new KP('TM1');

but I i simply include this file in other class function "require('t1.php')", the $K_printer is passed as NULL in LoadSetup function so it's throwing error. (I diderror_log(print_r($K_printer, true)); to capture this in error.log file of apache2.

[Thu Dec 12 02:31:15 2013] [error] [client 10.0.2.2] NULL\n
[Thu Dec 12 02:31:15 2013] [error] [client 10.0.2.2] Unknown printer setup.

really trying to find what is wrong since last night, finally wrote on stackoverflow to seek some expert advice.

289

Answer

Solution:

Change:

$K_printer = array(

to: $GLOBALS['$K_printer'] = array(

and:

 global $K_printer; 

to:

$K_printer = $GLOBALS['$K_printer'];

Doing so should fix the problem of the global variable $K_printer not existing in your class. I think doing it this way is not the best way to do it - I would have built it to send the array into the class function. But meh, I don't know what this code is doing and I think that doing the above will resolve it for you.

People are also looking for solutions to the problem: php - wordpress - Error 403 file javascript plugin wordpress

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.