<?php
/** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory */
protected $_collectionFactory;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
) {
$this->_collectionFactory = $collectionFactory;
}
public function yourMethod()
{
// Use factory to create a new product collection
$productCollection = $this->_collectionFactory->create();
/** Apply filters here */
$productCollection->addAttributeToSelect('*');
// Don't have to do this
// $productCollection->load();
foreach ($productCollection as $product){
echo 'Name = '.$product->getName().'<br>';
}
}