R
R
rena-nip2016-08-17 16:56:37
PHP
rena-nip, 2016-08-17 16:56:37

How to include the phpMorphy library in a class?

I am working with PhpMorhpy library. It comes with 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 which works with PhpMorphy. Previously, I carried out this initialization at the beginning of each class function. I understand that this is not correct, so I want to optimize the code. How to correctly initialize phpMorphy inside a class?
Thanks for the help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Inishev, 2016-08-17
@inish777

I am writing my own class which works with PhpMorphy.

The simplest option is to create an attribute that will store a phpmorphy instance and initialize it in the class constructor, then save the phpmorphy instance to a class attribute. Something like
class MorphyWrapper
{
    private $morphy;
    public function __construct()
    {
         //код инициализации
         $this->morphy = $morphy;  
    }
}

N
novrm, 2016-08-17
@novrm

Autoloading classes in PHP using SPL.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question