2

I had created a class Payment.php in the app directory of my laravel project, and I'm trying to use it in my HomeController.php controller . I had imported my payment class by using use App\Payment;statement, but it shows the following error

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'App\Payment' not found

app\http\controllers\ HomeController.php

<?php

namespace App\Http\Controllers;
use App\Payment;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index(){
        $payment = new Payment();
    }

}

app\ Payment.php

<?php
    class Payment {
        private $url;
        private $salt;
        private $params = array();

        public function __construct ( $salt, $env = 'test' )
        {
            $this->salt = $salt;
            switch ( $env ) {
            case 'test' :
                $this->url="https://testpay.easebuzz.in/";
                break;
            case 'prod' :
                $this->url = 'https://pay.easebuzz.in/';
                break;
            default :
                $this->url="https://testpay.easebuzz.in/";
            }
        }
    }
4
  • Add namespace App; above your class definition. Commented Mar 7, 2018 at 6:22
  • Generally model class are imported in the way you have used, but for custom class you need namespace as @AmitMerchant has said. Commented Mar 7, 2018 at 6:24
  • have a look on this : laracasts.com/discuss/channels/general-discussion/… Commented Mar 7, 2018 at 6:24
  • you are missing your namespace Commented Mar 7, 2018 at 6:27

2 Answers 2

2

You have to namespace the class. when you are adding a class which has to be imported by a namespace in some other class you should specify the namespace in the class declaration file.

App\Payment.php

<?php
    namespace App;

    class Payment {
        private $url;
        private $salt;
        private $params = array();

        ...
    }
Sign up to request clarification or add additional context in comments.

2 Comments

You should at least add some explanation for your answer. also no need to copy whole code for just a single line update.
posted the answer and was trying to edit it thanks for the concern :)
2

First of all try to learn the use of namespace

Namespaces and use are not Laravel features. These are part of Core PHP's functionality and can be used just like any other constructs. namespaces basically group your functions, classes and constants under a particular 'name', which we call a namespace.

namespace myself;
function hello(){
  echo 'hai';
}

Now if you include the php file consisting of the above code, you will now be able to call the function hello() in the following manner:

<?php
require 'myself.php';
myself\hello();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.