<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/ba2vwwqhx" -->

---
title: A story of compact() and extract() | daily.dev
description: A deep dive into PHP's compact() and extract() functions, covering how they work, their real-world usage patterns across 3000+ open source projects,...
canonical: https://daily.dev/posts/ba2vwwqhx
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: A story of compact() and extract() | daily.dev
og:description: A deep dive into PHP's compact() and extract() functions, covering how they work, their real-world usage patterns across 3000+ open source projects,...
og:url: https://daily.dev/posts/ba2vwwqhx
og:image: https://api.daily.dev/og/posts/ba2VWwqHX.png
og:image:alt: Post cover image
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# A story of compact() and extract()

**[PHP Dev](https://daily.dev/sources/phpdev)** · [@damienseguy](https://daily.dev/damienseguy) · 3 upvotes · 1 comments

## Summary

A deep dive into PHP's compact() and extract() functions, covering how they work, their real-world usage patterns across 3000+ open source projects, alternative syntaxes using variable variables and the spread operator, security risks of extract() with uncontrolled input, and practical recommendations for modernizing code that relies on them.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.exakat.io/a-story-of-compact-and-extract/>

## Community discussion

Top comments from developers on daily.dev.

**@rucaua** · 1 upvotes

> I’d prefer to avoid using `compact` and `extract` because they make the code less readable and less predictable. For example, this:
>
> ```
> $a = sizeDoesntMatter('big banana');
> $b = sizeDoesntMatter('small banana');
> $c = 47;
>
> $banana = compact('a', 'b', 'c');
> ```
>
> is less clear than writing it explicitly:
>
> ```
> $banana = [
>     'a' => sizeDoesntMatter('big banana'),
>     'b' => sizeDoesntMatter('small banana'),
>     'c' => 47,
> ];
> ```
>
> If u can't do that, probably u write spaghetti code. And this is the sign to refactor your ...
>
> And sometimes phpStan and your IDE not handle compact and extract

## Similar posts on daily.dev

- [JavaScript for Everyone: Destructuring](https://daily.dev/posts/javascript-for-everyone-destructuring-f7mq0z8t0) · CSS-Tricks · 18 upvotes · 0 comments
- [Twig 3.23: Introducing new operators and destructuring support \(Symfony Blog\)](https://daily.dev/posts/twig-3-23-introducing-new-operators-and-destructuring-support-symfony-blog--bk2cqn0wr) · Symfony · 0 upvotes · 0 comments
- [The Big Family](https://daily.dev/posts/the-big-family-ufhun8a2y) · The Daily WTF · 0 upvotes · 0 comments

---

Tags: [#security](https://daily.dev/tags/security), [#php](https://daily.dev/tags/php)

[View this post on daily.dev](https://daily.dev/posts/ba2vwwqhx)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/ba2vwwqhx","headline":"A story of compact() and extract()","text":"Shared: A story of compact() and extract()","url":"https://daily.dev/posts/ba2vwwqhx","datePublished":"2026-03-02T12:21:17.088Z","dateModified":"2026-03-02T12:21:57.007Z","author":{"@type":"Person","name":"Damien seguy","url":"https://daily.dev/damienseguy","image":"https://media.daily.dev/image/upload/s--nPbjKqol--/f_auto,q_auto/v1698422671/avatars/avatar_G85JhYcDxAg024Omc8Rlp","description":"PHP developer passionate about deep language knowledge, testing, static analysis, and sustainable it","worksFor":{"@type":"Organization","name":"Exakat","logo":"https://media.daily.dev/image/upload/s--ibtl6fze--/f_auto/v1749051132/companies/exakat"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":38990}},"image":"https://media.daily.dev/image/upload/s--ZrL_HSsR--/f_auto/v1722860399/public/Placeholder%2006","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"sharedContent":{"@type":"WebPage","url":"https://api.daily.dev/r/SwJvjJsLs"},"comment":[{"@type":"Comment","text":"I’d prefer to avoid using compact and extract because they make the code less readable and less predictable. For example, this:\n$a = sizeDoesntMatter(&#x27;big banana&#x27;);\n$b = sizeDoesntMatter(&#x27;small banana&#x27;);\n$c = 47;\n\n$banana = compact(&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;);\n\nis less clear than writing it explicitly:\n$banana = [\n    &#x27;a&#x27; =&gt; sizeDoesntMatter(&#x27;big banana&#x27;),\n    &#x27;b&#x27; =&gt; sizeDoesntMatter(&#x27;small banana&#x27;),\n    &#x27;c&#x27; =&gt; 47,\n];\n\nIf u can’t do that, probably u write spaghetti code. And this is the sign to refactor your …\nAnd sometimes phpStan and your IDE not handle compact and extract","datePublished":"2026-03-10T20:07:38.515Z","dateModified":"2026-03-10T20:40:35.735Z","url":"https://daily.dev/posts/ba2VWwqHX#c-3HGDEAarA","author":{"@type":"Person","name":"Maksym Tymofeiev","url":"https://daily.dev/rucaua","image":"https://media.daily.dev/image/upload/s--caTJyFHC--/f_auto/v1736549445/avatars/avatar_sIPMygD4Ld90iZsdsEqh7"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/phpdev","name":"PHP Dev"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PHP Dev","item":"https://daily.dev/squads/phpdev"},{"@type":"ListItem","position":3,"name":"A story of compact() and extract()"}]}
```

