<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Magento Expert</title>
	<atom:link href="https://www.the-mage-expert.com/tag/objectmanager/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.the-mage-expert.com</link>
	<description>Your Magento Commerce Expert</description>
	<lastBuildDate>Tue, 24 Nov 2026 08:23:52 +0000</lastBuildDate>
	<language>de-DE</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8.6</generator>
	<item>
		<title>Finding Direct ObjectManager Usage in Code</title>
		<link>https://www.the-mage-expert.com/3240/finding-direct-objectmanager-usage-in-code/</link>
		<comments>https://www.the-mage-expert.com/3240/finding-direct-objectmanager-usage-in-code/#comments</comments>
		<pubDate>Sat, 17 Oct 2026 20:07:01 +0000</pubDate>
		<dc:creator><![CDATA[Mex]]></dc:creator>
				<category><![CDATA[Magento Solutions]]></category>
		<category><![CDATA[Magento 2]]></category>
		<category><![CDATA[ObjectManager]]></category>

		<guid isPermaLink="false">https://www.the-mage-expert.com/?p=3240</guid>
		<description><![CDATA[In older extensions or inherited Magento code, you may find direct calls to the ObjectManager. A regular expression makes it easy to search for those calls throughout the codebase. [crayon-6b066cca3dd79/] For example, the search may find: [crayon-6b066cca3ddcd/] The regular expression&#8230;]]></description>
				<content:encoded><![CDATA[<p>In older extensions or inherited Magento code, you may find direct calls to the <strong>ObjectManager</strong>. A regular expression makes it easy to search for those calls throughout the codebase.</p><pre class="crayon-plain-tag">grep -RInP 'ObjectManager\s*::\s*getInstance\s*\(' app/code</pre><p>For example, the search may find:</p><pre class="crayon-plain-tag">$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productRepository = $objectManager-&gt;get(
    \Magento\Catalog\Api\ProductRepositoryInterface::class
);</pre><p>The regular expression accounts for different amounts of whitespace around <strong>::</strong> and before the opening parenthesis. The option <strong>-R</strong> performs a recursive search, <strong>-n</strong> prints line numbers, and <strong>-P</strong> enables grep&#8217;s Perl-compatible regular expressions.</p>
<p>You can also look for subsequent calls through a variable called <strong>$objectManager</strong> :</p><pre class="crayon-plain-tag">grep -RInP '\$objectManager\s*-&gt;\s*(get|create)\s*\(' app/code</pre><p>In custom Magento code, direct ObjectManager use is generally <strong>Bad Practice</strong>not recommended. The main reason is that it hides a class&#8217;s dependencies. You cannot see which services a class needs from its constructor, because it fetches them later from the ObjectManager.</p>
<p>That makes the code harder to understand and test. With dependency injection, you can replace a dependency with a mock object in a unit test. If a class retrieves the service itself from the global ObjectManager, the test must also account for that hidden access.</p>
<p>Changes become more difficult as well. A class that takes five services in its constructor clearly declares its dependencies. If it fetches those services across several methods, you must read the entire class to find them.</p>
<p>It is cleaner to pass dependencies through the constructor:</p><pre class="crayon-plain-tag">use Magento\Catalog\Api\ProductRepositoryInterface;

public function __construct(
    private ProductRepositoryInterface $productRepository
) {
}</pre><p>Magento then resolves the concrete implementation through dependency injection configuration. Preferences, proxies, plugins and other ObjectManager mechanisms still work, but your class does not need to know how a dependency is constructed.</p>
<p>The ObjectManager itself is not inherently bad: Magento relies on it internally. The problem is directly using it throughout application code, turning dependency injection into a hidden service locator.</p>]]></content:encoded>
			<wfw:commentRss>https://www.the-mage-expert.com/3240/finding-direct-objectmanager-usage-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
