DISQUS

interfacelab: Metadata/Attributes in PHP

  • Kevin · 1 year ago
    Thank you so much... You just saved me a ton of time.

    Any speed issues using this approach?
  • jawngee · 1 year ago
    Well, it definitely isn't blazing fast, but I don't believe the overhead is too taxing. That said, we use APC's shared memory cache to cache metadata so that we only really load it when the application starts up (the cache sticks around until you restart the server or until APC restarts itself). Using this strategy, once it's cached it's barely noticeable.

    You could do the same thing using memcached, but APC's is much faster as the memory is local to its process.
  • Kevin · 1 year ago
    I had an idea that might work out well in replacement for the format. Maybe using a JSON object and parsing it with json_decode. That way it's converted to an associated array on it's own without the need to convert it manually.
  • jawngee · 1 year ago
    JSON would be great here as well. Honestly, the only reason I chose YAML is because all of our other configuration is done with that.
  • quocbao · 1 year ago
    Your post was quite wonderful :D , I just translate the technique, give your example.

    Thanks for your nice post :)
  • Chris · 1 year ago
    That's an interesting technique.

    What I'm more interested in is why you left c#. I'm in the same boat. At work I do c# and it's pretty damned nice. I also maintain a boatload of wordpress blogs so I have to learn PHP.
  • iflyhigh · 1 year ago
    I'm the CTO for massify.com. When I signed on the site was being written in PHP. Unfortunately, it was a hideous wreck so we had to rewrite it, but thought we could use some of the code that had already been written. That wasn't the case however.

    Honestly, I don't mind it, there are some benefits to it, but I do miss C# and am sort of kicking myself for not having done the rewrite in Python.

    C'est la vie. :)
  • James Carr · 1 year ago
    That's VERY interesting
  • david · 1 year ago
    I use annotations with a array style notation like:

    /**
    * @someAnno(1=>2, "yeah"=> array("no","yes"))
    */

    It's really too bad that the language itself doesn't support it (outside doc comments) as it would be much faster and not require the caching.
  • jgmassify · 1 year ago
    Nice one.

    Yeah, it's unfortunate that this is really a huge hack to get a language feature that would be a nice to have.

    I'm going to be re-posting a patch for PHP that enables array shortcut notation in the next couple of days. The patch was originally by Ryusuke Sekiyama, posted here: http://marc.info/?l=php-internals&m=11999597202...

    Instead of using array() you can use [], eg:

    $var=[1,2,3,4];
    $othervar=[ 'test' => [1,2], 'sick' => [1,[3,4]]];
  • PCSpectra · 1 year ago
    Excellent idea...in fact dare I say brilliant? :P

    I've been poking around for a while now trying to figure out a way to use reflection that would be "interesting" and "innovative" I think this might be it.

    The part where you said, using reflection to obtain meta data and have that information be discoverable rather than declarative...I'm not sure I agree 100% but the idea of marking my methods as REST-able (for lack of a better word) by indicating that in the comment as meta-data...

    Damn thats a good idea and I owe it all to this article. :P

    Cheers,
    Alex