Magento2 Error – Fix Fatal Error: Cannot Declare Class Product

magento2

I have try to create custom module in magento 2 but i have got error like

Fatal error: Cannot declare class Product, because the name is already in use in /var/www/mymagento/app/code/Plus/Demo/Model/Product.php on line 2

Here is my Product.php

<?php
class Product {     
public function sayHello() {
    echo "hi";
  }
}

And here is my test.php

<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$quote = $obj->get('Plus\Demo\Model\Product');   
//I have created say fucntion under Product.php file
echo '<pre>';
print_r($quote->sayHello());
echo '</pre>'; */

I tried but i still can not success.

Thanks in advance

TRY

Best Answer

Add namespace in Product.php

app/code/Plus/Demo/Model/Product.php

<?php

namespace Plus\Demo\Model;

class Product
{    
    public function sayHello()
    {
        echo "hi";
    }
}
Related Topic