1

I have defined services and DAO class (to access DB) in my laravel projects. I need to create an object of the class that can access through functions. Here is the way I tried it? but it's not working

<?php
namespace App\Http\Controllers;

use App\DAO\CategoryDAO;

$categoryDAO = new CategoryDAO();

function categoryReadAll()
{

    return $CategoryDAO->categoryReadAll();

}

How can I do this?

1
  • What is "this"? Looks like a pretty usual way of global variables which should be avoided nowadays Commented Mar 25, 2020 at 8:03

2 Answers 2

1

Also, this one works fine

  $GLOBALS['auctionHouseDAO'] = new AuctionHouseDAO(); 

  function createAuctionHouse($request) { 
      global $auctionHouseDAO;
      return $auctionHouseDAO->createAuctionHouse($request); 
 }
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this way:

namespace App\Http\Controllers;
use App\DAO\CategoryDAO;

$categoryDAO = new CategoryDAO();
$GLOBALS['categoryDAO'] = $categoryDAO;

function categoryReadAll() {

    return $GLOBALS['categoryDAO']->categoryReadAll();

}

Check the documentation

Or:

namespace App\Http\Controllers;
use App\DAO\CategoryDAO;

$categoryDAO = new CategoryDAO();

function categoryReadAll() {
    global $categoryDAO;
    return $categoryDAO->categoryReadAll();

}

Hope help you.

1 Comment

Thank you very much. Only the second one works for me.

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.