TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Oink: An API for PHP in a single file

60 pointsby juancroldanover 1 year ago

16 comments

superasnover 1 year ago
I think this is the reason why PHP is still so popular. Even with the latest 8.3 version these things still work - even though everyone else has moved to frameworks like laravel or fully OO code.<p>Sometimes you just want to get shit done for your personal weekend hobby projects and little things like this can be a tremendous help to make your MVP. Sometimes these MVPs can be super practical in real-life as well (1)<p>Writing code this way can sometimes be liberating and more fun like freestyle swimming. I still miss the days of writing native SQL queries instead of using the ORM for everything nowadays.<p>(1) <a href="https:&#x2F;&#x2F;twitter.com&#x2F;levelsio&#x2F;status&#x2F;1381709793769979906?lang=en" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;levelsio&#x2F;status&#x2F;1381709793769979906?lang...</a>
评论 #39368533 未加载
评论 #39370179 未加载
ragnarussover 1 year ago
Reading the code, this feels like a python developer writing PHP and frankly doing a bad job. Even the formatting seems to give up readability for line count for some unknown reason. There is no typing, no following of PSR-2 in anyway, and things which make my static analysis tools very unhappy.<p>A single file API is not a bad idea, but this is a bad implementation.
评论 #39368732 未加载
评论 #39369310 未加载
评论 #39368889 未加载
progreover 1 year ago
Nice! Here is my own take on the same idea (single file PHP api)<p><a href="https:&#x2F;&#x2F;github.com&#x2F;prog-re&#x2F;ugly&#x2F;blob&#x2F;main&#x2F;docs&#x2F;manual.md">https:&#x2F;&#x2F;github.com&#x2F;prog-re&#x2F;ugly&#x2F;blob&#x2F;main&#x2F;docs&#x2F;manual.md</a><p>Edit: I added links to Oink and Zaap from my readme.md. We could make a Single file PHP api framework webring!
评论 #39368300 未加载
toredover 1 year ago
Personally I believe that you should always use <i>require</i> (<i>require_once</i>) instead of <i>include</i> (<i>include_once</i>) to be sure that a failure to load file creates an <i>E_ERROR</i> instead of an <i>E_WARNING</i>, this both for loading the framework but also when the framework when loading the api file.<p><i>E_ERROR</i> is a fatal error thus it will throw (I think since PHP 7) an <i>Error</i> exception that can be catched with a normal try&#x2F;catch and thus possible to return a proper response.
评论 #39369595 未加载
p4bl0over 1 year ago
The title is quite confusing on what the project is. It should be &quot;Oink: a single-file PHP library to easily build API&quot; or something like that.<p>Appart from that, it&#x27;s a nice project, but would benefit from better examples that actually needs an API to do things asynchronously, like a web app that needs to be able to synchronize with a server from time to time. Maybe a TODO list to keep the example short? Because the blog and gallery examples are not really convincing: why would we depend on JS to render the page and almost do the job twice while the rendering could have easily be done in PHP directly?
评论 #39367694 未加载
评论 #39367437 未加载
cynicalsecurityover 1 year ago
Please god no. People might think this is what PHP looks like in real life while it&#x27;s far from the good practices that are normally used in PHP since ages.
评论 #39368036 未加载
评论 #39368175 未加载
huqedatoover 1 year ago
That&#x27;s a gem! It reminds me why I used to love PHP: rapid prototyping, functional style. It&#x27;s a shame that I don&#x27;t use it for a long time.
andridkover 1 year ago
Neat! Any plans to support OpenAPI schema? It&#x27;s cool that it&#x27;s easy to create the API, but consuming and documenting it is also a factor.
评论 #39369614 未加载
评论 #39367436 未加载
morayover 1 year ago
That&#x27;s a nice little project that I would use to quickly build an API for testing or developing an app, you just need the php executable to start it up.<p>But if I had to make one comment, after looking at the code, that serve function with that huge try&#x2F;catch block is not the nicest thing to see, I would split it up and try to get some more meaningful exception and error handling.
aaviator42over 1 year ago
Hey, nice! Here&#x27;s my take on the same concept: <a href="https:&#x2F;&#x2F;github.com&#x2F;aaviator42&#x2F;zaap">https:&#x2F;&#x2F;github.com&#x2F;aaviator42&#x2F;zaap</a><p>Allows me to have an API with multiple methods and endpoints up and running in minutes. :)
geenatover 1 year ago
All it really takes in PHP: <a href="https:&#x2F;&#x2F;github.com&#x2F;gnat&#x2F;api.php&#x2F;blob&#x2F;main&#x2F;api.php">https:&#x2F;&#x2F;github.com&#x2F;gnat&#x2F;api.php&#x2F;blob&#x2F;main&#x2F;api.php</a>
slimover 1 year ago
This is cool but, I can make an API for PHP in zero file. It would be simpler and cleaner.
CR007over 1 year ago
I love PHP but I hate the code in this repo.
评论 #39368247 未加载
hiccuphippoover 1 year ago
I&#x27;ve never seen `use function Namespace\{functionName}` before, is that autoloadable with PSR-4?
评论 #39369902 未加载
pachicoover 1 year ago
I just honestly hope this doesn&#x27;t turn into another rant against PHP, as it usually happens.
评论 #39367979 未加载
purerandomnessover 1 year ago
You don&#x27;t need this PHP snippet:<p>To get the same functionality without the extra step, simply use PostgREST [1]<p>[1] <a href="https:&#x2F;&#x2F;postgrest.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;postgrest.org&#x2F;</a>