how can add external php function or class to theme file?
A .theme file is processed as PHP - anything you can do in a PHP file, you can do in your theme's .theme file.

So if you want a service:

$some_service = \Drupal::service('foo');


Or a function from a module file:

module_load_include('inc', 'foo_module');


And so on.