<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mahmoud Ragaey on Medium]]></title>
        <description><![CDATA[Stories by Mahmoud Ragaey on Medium]]></description>
        <link>https://medium.com/@mahmoud.ragaey.99?source=rss-c208b3b70696------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MONr0xUANOFKcW-s9I2k3g.jpeg</url>
            <title>Stories by Mahmoud Ragaey on Medium</title>
            <link>https://medium.com/@mahmoud.ragaey.99?source=rss-c208b3b70696------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 19:00:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mahmoud.ragaey.99/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[DTO is Not Just an Array: What Most Developers Get Wrong]]></title>
            <link>https://medium.com/@mahmoud.ragaey.99/dto-is-not-just-an-array-what-most-developers-get-wrong-1d275a1e9d3c?source=rss-c208b3b70696------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d275a1e9d3c</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[php-developers]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Mahmoud Ragaey]]></dc:creator>
            <pubDate>Wed, 18 Mar 2026 16:53:41 GMT</pubDate>
            <atom:updated>2026-03-18T16:53:41.441Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Data Transfer Object (DTO) illustrating controlled data flow between application layers instead of passing models directly" src="https://cdn-images-1.medium.com/max/1024/1*-Y63DIujiVlOr2VgzhKezA.png" /></figure><p>For a long time, I thought passing data in PHP was simple… just use an array and move on.</p><p>Until one day, a small bug in a wallet transfer feature cost me hours of debugging.</p><p>Missing keys, unexpected data types, and tightly coupled logic made the system fragile.</p><p>That’s when I realized:<br><em>DTO is not just a “cleaner array”… it’s a contract.</em></p><pre>class TransferData<br>{<br>    public function __construct(<br>        public int $fromId,<br>        public int $toId,<br>        public float $amount,<br>        public ?int $orderId = null<br>    ) {}<br>}</pre><h3>Why This “Contract” Matters</h3><p>When you use a DTO, you’re not just moving data … You are explicitly defining <strong>what is allowed to move between layers</strong>.</p><h4><strong>1.Controlling the Data ShapeWith a DTO, you define:</strong></h4><ul><li>The exact keys you want to use</li><li>Which data is required</li><li>The expected data types</li></ul><p>In simple terms:</p><blockquote><em>You don’t send everything… you send only what you intend to send.</em></blockquote><p>This naturally isolates any data that is:</p><ul><li>Unnecessary</li><li>Sensitive</li><li>Not meant to be exposed<br>And here’s an important advantage:</li></ul><blockquote><em>If you try to use or access a property that doesn’t exist in the DTO…<br> your application will fail fast with an exception.</em></blockquote><p>Unlike arrays, where missing keys might silently pass or cause undefined behavior, DTOs make errors explicit and immediate.</p><p>This means:</p><ul><li>No silent bugs</li><li>No guessing</li><li>No hidden issues</li></ul><blockquote><em>You know instantly when something is wrong.</em></blockquote><pre>$dto-&gt;unknownField; // ❌ Exception</pre><h4><strong>2. Passing Models Directly Between Layers</strong></h4><p>Another common approach is to pass the Model itself between layers.</p><p>For example:</p><pre>$walletService-&gt;transfer($wallet); </pre><p>At first, this might look like a clean and convenient solution.</p><p>But here’s what actually happens behind the scenes…</p><h3>What Are You Really Passing?</h3><p>When you pass a model (like an Eloquent model), you are not just passing data.</p><p>You are passing:</p><ul><li>All attributes from model</li><li>All relationships</li><li>All built-in behaviors (save, update, delete, etc.)</li></ul><blockquote><em>In other words, you are exposing much more than you actually need.</em></blockquote><h4>What Problems Can This Cause?</h4><p>Because the model contains behavior, not just data … any layer that receives it now has the ability to:</p><ul><li>Modify the data directly</li><li>Persist changes to the database</li><li>Trigger model events or hooks</li></ul><p>For example:</p><pre>$wallet-&gt;balance -= 100;<br>$wallet-&gt;save();</pre><p>This might happen inside a Service or even another layer where it doesn’t belong.</p><p><strong>Why This Is Dangerous ?<br></strong>The issue here is not just flexibility … It’s <strong>lack of control </strong>You are allowing any layer to:</p><ul><li>Perform database operations</li><li>Change state unexpectedly</li><li>Introduce side effects without clear boundaries</li></ul><p>And the worst part?</p><blockquote><em>These changes can happen silently and be very hard to trace.</em></blockquote><p><strong>The Key Insight</strong></p><blockquote><em>When you pass a Model, you are not just sharing data…<br> you are sharing power.</em></blockquote><p>And without clear boundaries, that power can easily be misused.</p><h4>Why DTO Fixes This</h4><p>This is exactly where DTOs shine.</p><p>Instead of passing a full model, you pass only the data you need.</p><ul><li>No behaviors</li><li>No database access</li><li>No unexpected side effects</li></ul><blockquote><em>You move from “anything can happen” → to “only what is defined can happen”</em></blockquote><h3>Naming DTOs the Right Way</h3><p>Choosing the right name for your DTO is more important than it seems.</p><p>A good DTO name should clearly describe its purpose… not just the data it holds.</p><h4>Common Mistake</h4><p>Using generic or vague names like:</p><pre>Data<br>Info<br>Payload<br>RequestData</pre><h4>Better Approach</h4><p>Name your DTO based on the <strong>action or use case</strong>:</p><pre>TransferDataDTO<br>CreateUserDTO<br>UpdateOrderDTO</pre><h3>When NOT to Use DTOs</h3><p>DTOs are powerful… but they are not always necessary.</p><p>Using them everywhere can lead to over-engineering.</p><h4>1. Simple CRUD Operations</h4><p>If your code is just:</p><ul><li>Receiving data</li><li>Saving it directly</li><li>No business logic involved</li></ul><p>Then a DTO might be unnecessary overhead.</p><h4>2. Very Small or Temporary Projects</h4><p>If you’re building:</p><ul><li>A quick prototype</li><li>A small internal tool</li></ul><p>You might not need the extra abstraction.</p><h4>3. No Clear Layer Separation</h4><p>If your application doesn’t have:</p><ul><li>Service layer</li><li>Clear architecture</li></ul><p>Then DTOs won’t give you their full value.</p><h3>Final Thought</h3><blockquote><em>DTOs are not about adding more classes…<br> They are about adding clarity and control.</em></blockquote><p>Use them when they bring value…<br> and avoid them when they don’t.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d275a1e9d3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>