Getting error on adding an attribute programmatically in Magento 2

A Khan
A Khan used Ask the Experts™
on
I am trying to add an attribute programmatically in Magento 2 but getting error. Please I need urgent help.
Thanks in advance. Details are as below:


I followed the instructions on https://www.atwix.com/magento/adding-attribute-programatically-magento2/ and tried to add attribute.
I have windows 7,xampp with php 7.0, magento 2.1 and mysql.
in path variable I set following for php commands:
C:\xampp\php;C:\xampp\php\php.exe

When I run
C:\xampp\htdocs\maglocal1>php bin/magento setup:upgrade
I get following error at the end

Parse error: syntax error, unexpected 'use' (T_USE) in C:\xampp\htdocs\maglocal1
\app\code\Atwix\TestAttribute\Setup\InstallData.php on line 5

My data in \app\code\Atwix\TestAttribute\Setup\InstallData.php is

<?php
namespace Atwix\TestAttribute\Setup;

 
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
 
/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
 
    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
 
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 
        /**
         * Add attributes to the eav/attribute
         */
 
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'test_attribute',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Test Attribute',
                'input' => '',
                'class' => '',
                'source' => '',
                'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I solved the issue. I tested on new installation and got new error.
That is solved by replacing \Magento\Catalog\Model\Model\Eav\Attribute with \Magento\Catalog\Model\ResourceModel\Eav\Attribute

Regards,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial