Get Instance ID of an Object in PHP

前端 未结 10 1678
说谎
说谎 2020-12-14 07:19

I\'ve learn a while ago on StackOverflow that we can get the \"instance ID\" of any resource, for instance:

var_dump(intval(curl_init()));  // int(2)
var_dum         


        
10条回答
  •  死守一世寂寞
    2020-12-14 07:55

    What you're trying to do, is actually Aspect-Oriented Programming (AOP).

    There are at least a couple of frameworks available for AOP in PHP at this point:

    • seasar (formerly PHPaspect) is a larger framework integrating with Eclipse - the screenshot shows you a little code snippet that answers your question, weaving some code around a particular new statement throughout a project.
    • php-aop is a lightweight framework for AOP.
    • typo3 has an AOP framework built in.

    This may be overkill for your needs, but you may find that exploring the kind of thinking behind ideas like these will lead you down the rabbithole, and teach you new ways to think about software development in general - AOP is a powerful concept, allowing you to program in terms of strategies and concerns, or "aspects".

    Languages like PHP were designed to solve programming tasks - the concept of APO was designed to solve a programmer's tasks. When normally you would need to think about how to ensure that a particular concern gets fulfilled every time in your codebase, you can think of this as simply an "aspect" of how you're programming, implement it in those terms directly, and count on your concerns to be implemented every time.

    It requires less discipline, and you can focus on solving the practical programming tasks rather than trying to architect your way through high-level structural code requirements.

    Might be worth 5 minutes of your time, anyway ;-)

    Good luck!

提交回复
热议问题