0
votes

So I'm using more specifically Elasticsearch which is available through composer. It is the 5.0 version. I'm trying to build the client connection inside a private function inside a class. Basically what I want is 1 class with 2 private functions. The first private function will create the client connection using:

$hosts = ['host' => 'http://censored'];
$clientBuilder = ClientBuilder::create();
$clientBuilder->setHosts($hosts);
$clientBuilder->setSerializer('\Elasticsearch\Serializers\EverythingToJSONSerializer');
$client = $clientBuilder->build();
$searchParams = ['index' => 'main'];

Mind you that $searchparams and $client will have to be accessible by the second private function mentioned below.

The second private function will take in a type and query from the public function and use $client and $searchparam to initiate a API request to Elasticsearch

Summary: The public function will be called from another file. To use it it will require a type ($type) and query ($query) to be passed, which will be passed to the second private function. The second private function will initiate a API request to Elasticsearch and give back the results to the public function, which then the public function will echo out the results back to the user.

I believe this is possible, but I don't know how to call or use another namespace within a private function. Putting that into a private function and autoloading Elasticsearch beforehand results in ClientBuilder not being found.

Thank you in advance and sorry for the long post.

1

1 Answers

0
votes

it doesn't matter where you call it, use is the exact same...

for example file foo.php:

namespace Foo\Bar;

function Baz() { echo 'Baz()...'; }

class Qux
{
    public function         PublicMethod() {
        echo 'PublicMethod()...';
    }
    public static function  StaticMethod() {
        echo 'StaticMethod()...';
    }

}

you could use unqualified name if you are in same namespace or use namespace:

namespace Foo\Bar;
include 'foo.php';

Baz();

Qux::StaticMethod();

$qux = new Qux();
$qux->PublicMethod();

or

namespace MyNamespace;
include 'foo.php';

use function Foo\Bar\Baz;
use Foo\Bar\Qux;

Baz();

Qux::StaticMethod();

$qux = new Qux();
$qux->PublicMethod();

or qualified name

namespace MyNamespace;
include 'foo.php';

use Foo\Bar;

Bar\Baz();

Bar\Qux::StaticMethod();

$qux = new Bar\Qux();
$qux->PublicMethod();

or fully qualified name

include 'foo.php';

\Foo\Bar\Baz();

\Foo\Bar\Qux::StaticMethod();

$qux = new \Foo\Bar\Qux();
$qux->PublicMethod();

backslash \ on the beggining mean global