PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 7, 2022

[FIXED] Composer Uncaught Error: Class 'Instasent\SMSCounter\SMSCounter' not found

 January 07, 2022     composer-php, installation, php     No comments   

Issue

i have folder /home/wira/wirdev/smscounter, in that folder i'm installing sms counter from https://github.com/instasent/sms-counter-php , installation completed. now inside of my folder is like this :

.
├── checkSMSSegment.php
├── composer.json
├── composer.lock
├── sms.txt
├── tesSMSLength.php
└── vendor
    ├── autoload.php
    ├── composer
    │   ├── autoload_classmap.php
    │   ├── autoload_namespaces.php
    │   ├── autoload_psr4.php
    │   ├── autoload_real.php
    │   ├── autoload_static.php
    │   ├── ClassLoader.php
    │   ├── installed.json
    │   └── LICENSE
    └── instasent
        └── sms-counter-php
            ├── composer.json
            ├── LICENSE-MIT
            ├── phpunit.xml.dist
            ├── README.md
            ├── SMSCounter.php
            └── Tests
                └── SMSCounterTest.php

this is code i want to run (tesSMSLength.php) :

<?php
use Instasent\SMSCounter\SMSCounter;

$text = file_get_contents(dirname(__FILE__)."/sms.txt");

$smsCounter = new SMSCounter();
$result = $smsCounter->count($text);

print_r($result);

Now i want to run my code using this library by typing sudo php tesSMSLength.php, but it shows ERROR class not found like this :

[wira@DevWira smscounter]$ sudo php tesSMSLength.php
PHP Fatal error:  Uncaught Error: Class 'Instasent\SMSCounter\SMSCounter' not found in /home/wira/wirdev/smscounter/tesSMSLength.php:6
Stack trace:
#0 {main}
  thrown in /home/wira/wirdev/smscounter/tesSMSLength.php on line 6

What i'm missing here?

this is composer.json contents :

{
    "require": {
       "instasent/sms-counter-php": "^0.4"
    },
    "autoload": {
        "psr-4": {
            "Instasent\\SMSCounter\\": ""
        }
    },
}

Solution

If your code is a CLI script, you're missing the autoload import at the top of the file:

<?php

require 'vendor/autoload.php';

use Instasent\SMSCounter\SMSCounter;

...

Also, since instasent/sms-counter-php already includes autoload configuration in its composer.json file, you don't have to include it yourself in your composer.json.



Answered By - Edi Modrić
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing