1

I am using slim framework 2 with medoo via composer, i am making singleton for medoo but when i call the medoo class to configure my db info, so it gives me the fatal error like below

Fatal error: Class 'medoo' not found in C:\xampp\htdocs\school\s.php on line 5

below is my s.php file

<?php
  require 'vendor/autoload.php';
  $app = new\Slim\Slim();
    $app->container->singleton('test',function () use ($app) {
      return new medoo([
        'database_type' =>'mysql',
        'database_name' =>'mydb',
        'server'=> 'localhost',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
        'option' => [
          PDO::ATTR_CASE=>PDO::CASE_NATURAL
        ]
      ]);
    });

    $app->get('/', function () use($app) {
      echo "<center><b><a href='#' target='_blank' >WELCOME TO TESTING PAGE</a></b></center>";
      $sth = $app->test->insert("t", ["id" =>1, "name" => "dsfdsf"]);
      var_dump($sth);
    });

  $app->run();
?> 

If i check the composer.json file then i find slim and medoo both are there, i am not getting why this fatal error is coming please help me

lazyCoder
  • 2,544
  • 3
  • 22
  • 41

1 Answers1

4

Two things:

  • you need to import the class
  • your class name should be case-sensitive

That is:

<?php

use Medoo\Medoo;

require 'vendor/autoload.php';

$app = new \Slim\Slim();

$app->container->singleton('test',function () use ($app) {
    return new Medoo([
        // ...
    ]);
});

For reference, see:

localheinz
  • 9,179
  • 2
  • 33
  • 44
  • then there will be no sense for using singleton, if use namespace medoo every time – lazyCoder Jul 16 '17 at 04:39
  • thanks , but i did it without use namespace medoo for singleton – lazyCoder Jul 16 '17 at 04:44
  • What version of `catfan/medoo` are you using? – localheinz Jul 16 '17 at 04:46
  • medoo version is 1.4 latest one – lazyCoder Jul 16 '17 at 04:49
  • What do you mean by *did it without use namespace medoo for singleton*? – localheinz Jul 16 '17 at 04:52
  • The namespace of a class name used for constructing an instance of a service has nothing to do with the concept of a *singleton* within the context of service containers. A singleton service just means that - regardless of how often you try to fetch a service from a container - the same instance will be returned (as opposed to a new one every time). See http://docs.slimframework.com/di/overview/#singleton-resources. – localheinz Jul 16 '17 at 05:03
  • You don't need to `use Meedo\Meedo` every time, just where you're creating a new instance. This answer is answer is correct. Also I'm curious to know how did you manage to create a new Meedo instance without using the namespace this class is defined under? – Nima Aug 13 '17 at 15:46
  • @Nima Are you asking me or the original poster? – localheinz Aug 13 '17 at 15:58
  • I'm asking the original poster. Sorry, I forgot to mentioned him. – Nima Aug 13 '17 at 16:10