I work with the library PhpMorhpy. Attached is the following initialization example:

require_once(dirname(__FILE__) . '/../src/common.php'); // set some options $opts = array( // storage type, follow types supported // PHPMORPHY_STORAGE_FILE - use file operations(fread, fseek) for dictionary access, this is very slow... // PHPMORPHY_STORAGE_SHM - load dictionary in shared memory(using shmop php extension), this is preferred mode // PHPMORPHY_STORAGE_MEM - load dict to memory each time when phpMorphy intialized, this useful when shmop ext. not activated. Speed same as for PHPMORPHY_STORAGE_SHM type 'storage' => PHPMORPHY_STORAGE_FILE, // Extend graminfo for getAllFormsWithGramInfo method call 'with_gramtab' => false, // Enable prediction by suffix 'predict_by_suffix' => true, // Enable prediction by prefix 'predict_by_db' => true ); // Path to directory where dictionaries located $dir = dirname(__FILE__) . '/phpmorphy-0.3.7/dicts'; // Create descriptor for dictionary located in $dir directory with russian language $dict_bundle = new phpMorphy_FilesBundle($dir, 'rus'); // Create phpMorphy instance try { $morphy = new phpMorphy($dict_bundle, $opts); } catch(phpMorphy_Exception $e) { die('Error occured while creating phpMorphy instance: ' . $e->getMessage()); } 

I am writing my own class that works with PhpMorphy. Earlier, I performed this initialization at the beginning of each class function. I understand that this is not correct, so I want to optimize the operation of the code. How to correctly initialize phpMorphy inside a class?

Thanks for the help!

    1 answer 1

    If you need one instance of a class, you can do it through a hetero;

     private $morph = null; public function getMorph() { if(!is_null($this->morph) { return $this->morph; } // set some options $opts = array( // storage type, follow types supported // PHPMORPHY_STORAGE_FILE - use file operations(fread, fseek) for dictionary access, this is very slow... // PHPMORPHY_STORAGE_SHM - load dictionary in shared memory(using shmop php extension), this is preferred mode // PHPMORPHY_STORAGE_MEM - load dict to memory each time when phpMorphy intialized, this useful when shmop ext. not activated. Speed same as for PHPMORPHY_STORAGE_SHM type 'storage' => PHPMORPHY_STORAGE_FILE, // Extend graminfo for getAllFormsWithGramInfo method call 'with_gramtab' => false, // Enable prediction by suffix 'predict_by_suffix' => true, // Enable prediction by prefix 'predict_by_db' => true ); // Path to directory where dictionaries located $dir = dirname(__FILE__) . '/phpmorphy-0.3.7/dicts'; // Create descriptor for dictionary located in $dir directory with russian language $dict_bundle = new phpMorphy_FilesBundle($dir, 'rus'); // Create phpMorphy instance try { $morphy = new phpMorphy($dict_bundle, $opts); } catch(phpMorphy_Exception $e) { die('Error occured while creating phpMorphy instance: ' . $e->getMessage()); } } 

    or in class constructs

     public function __construct() { // set some options $opts = array( // storage type, follow types supported // PHPMORPHY_STORAGE_FILE - use file operations(fread, fseek) for dictionary access, this is very slow... // PHPMORPHY_STORAGE_SHM - load dictionary in shared memory(using shmop php extension), this is preferred mode // PHPMORPHY_STORAGE_MEM - load dict to memory each time when phpMorphy intialized, this useful when shmop ext. not activated. Speed same as for PHPMORPHY_STORAGE_SHM type 'storage' => PHPMORPHY_STORAGE_FILE, // Extend graminfo for getAllFormsWithGramInfo method call 'with_gramtab' => false, // Enable prediction by suffix 'predict_by_suffix' => true, // Enable prediction by prefix 'predict_by_db' => true ); // Path to directory where dictionaries located $dir = dirname(__FILE__) . '/phpmorphy-0.3.7/dicts'; // Create descriptor for dictionary located in $dir directory with russian language $dict_bundle = new phpMorphy_FilesBundle($dir, 'rus'); // Create phpMorphy instance try { $this->morph = new phpMorphy($dict_bundle, $opts); } catch(phpMorphy_Exception $e) { die('Error occured while creating phpMorphy instance: ' . $e->getMessage()); } } 

    How to use? the first option if you do getterom obvorachivaya in some kind of class then it will be Tipo

     $myMorph = new MyMorph(); $moph = $myMorph->getMorph(); 

    in the second case, you have to initialize in the constructor of each class and get the class through $this

     $this->morph; 
    • And where to enter it: require_once (dirname ( FILE ). '/../Src/common.php'); to class or to? - Rena Nip
    • @RenaNip either at the beginning of the file or in the autoloader file, if any. what is the project? - Naumov
    • while fully on php, will be transferred to the framework - Rena Nip
    • @RenaNip Need to look at the structure in general, the mind needs an autoloader for such purposes, is the entry point index.php or is the page a separate file? - Naumov
    • while a separate file. the structure is as follows - the script loads my class through require, and my class also loads phpMorphy - Rena Nip