Get Instance ID of an Object in PHP

前端 未结 10 1611
说谎
说谎 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!

    0 讨论(0)
  • 2020-12-14 07:58

    As long as you implement the base class all the classes you're going to need this from, you can do something like this:

    class MyBase
    {
        protected static $instances = 0;
        private $_instanceId  = null;
        public function getInstanceId()
        {
            return $this->_instanceId;
        }
    
        public function __construct()
        {
            $this->_instanceId = ++self::$instances;
        }
    }
    
    class MyTest extends MyBase
    {
        public function Foo()
        {
            /* do something really nifty */
        }
    }
    
    $a = new MyBase();
    $b = new MyBase();
    
    $c = new MyTest();
    $d = new MyTest();
    
    
    printf("%d (should be 1) \n", $a->getInstanceId());
    printf("%d (should be 2) \n", $b->getInstanceId());
    printf("%d (should be 3) \n", $c->getInstanceId());
    printf("%d (should be 4) \n", $d->getInstanceId());
    

    The output would be:

    1 (should be 1) 
    2 (should be 2) 
    3 (should be 3) 
    4 (should be 4) 
    
    0 讨论(0)
  • 2020-12-14 08:03

    spl_object_hash() could help you out here. It

    returns a unique identifier for the object

    which is always the same for a given instance.

    EDIT after OP comment:

    You could implement such a behavior using a static class property, e.g:

    class MyClass 
    {
        private static $_initialized = false;
    
        public function __construct()
        {
            if (!self::$_initialized) {
                self::$_initialized = true;
                // your run-only-once code 
            }
        }
    }
    

    But actually this has nothing to with your original question.

    0 讨论(0)
  • 2020-12-14 08:06

    As of PHP 7.2 you can use spl_object_id

    $id = spl_object_id($object);
    $storage[$id] = $object;
    
    0 讨论(0)
提交回复
热议问题