Test for the existence of a null property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wildernesscat@gmail.com

    Test for the existence of a null property

    Hello there,

    I'm looking for a method to test, whether an object has a certain
    property.
    Consider the following snippet:

    class A { var $aaa; }
    $var = new A;

    (Assuming that the structure of class A is unknown) I need a way to
    check whether $var->aaa exists (test positive), and whether $var->xxx
    exists (test negative). I tried boolean tests, isset(), is_null(), but
    they can't tell the difference.
    I guess I could convert the object to an array and test its indices,
    but that wouldn't be practical for large object with many fields.

    Any other ideas?

    Thanks in advance,
    Danny

  • Jerry Stuckle

    #2
    Re: Test for the existence of a null property

    wildernesscat@g mail.com wrote:
    Hello there,
    >
    I'm looking for a method to test, whether an object has a certain
    property.
    Consider the following snippet:
    >
    class A { var $aaa; }
    $var = new A;
    >
    (Assuming that the structure of class A is unknown) I need a way to
    check whether $var->aaa exists (test positive), and whether $var->xxx
    exists (test negative). I tried boolean tests, isset(), is_null(), but
    they can't tell the difference.
    I guess I could convert the object to an array and test its indices,
    but that wouldn't be practical for large object with many fields.
    >
    Any other ideas?
    >
    Thanks in advance,
    Danny
    >
    Danny,

    Once of the concepts of OO programming is encapsulation (not fully
    implemented in PHP), in which can't access $aaa. Anything outside of
    the class should not have access to the internal variables of the class;
    nor should they concern themselves with the internals of the class.
    This limits the effects of changes to the class.

    Exactly what problem are you trying to resolve?


    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • wildernesscat

      #3
      Re: Test for the existence of a null property

      Hi Jerry,

      I'm trying to write a function that checks whether a given object is of
      a known class (named 'A' in this example). I want it to work even if
      the object has been constructed manually, starting from 'new
      StdClass()'.

      Danny


      Jerry Stuckle wrote:
      wildernesscat@g mail.com wrote:
      Hello there,

      I'm looking for a method to test, whether an object has a certain
      property.
      Consider the following snippet:

      class A { var $aaa; }
      $var = new A;

      (Assuming that the structure of class A is unknown) I need a way to
      check whether $var->aaa exists (test positive), and whether $var->xxx
      exists (test negative). I tried boolean tests, isset(), is_null(), but
      they can't tell the difference.
      I guess I could convert the object to an array and test its indices,
      but that wouldn't be practical for large object with many fields.

      Any other ideas?

      Thanks in advance,
      Danny
      >
      Danny,
      >
      Once of the concepts of OO programming is encapsulation (not fully
      implemented in PHP), in which can't access $aaa. Anything outside of
      the class should not have access to the internal variables of the class;
      nor should they concern themselves with the internals of the class.
      This limits the effects of changes to the class.
      >
      Exactly what problem are you trying to resolve?
      >
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Marcin Dobrucki

        #4
        Re: Test for the existence of a null property

        wildernesscat wrote:
        Hi Jerry,
        >
        I'm trying to write a function that checks whether a given object is of
        a known class (named 'A' in this example). I want it to work even if
        the object has been constructed manually, starting from 'new
        StdClass()'.
        I think this is VERY different from what you asked in the first mail.
        But to do that, you want something like this:

        class Foo (
        function Foo() {}
        }

        $f = new Foo();
        ....
        if (is_a($f, 'Foo')) {
        echo "yes";
        }
        ....
        echo "\$f is of type: " . get_class($f);

        To do what you asked before (and hence break some main concepts in
        OO), you can:

        $vars = get_class_vars( 'Foo');

        /M

        Comment

        • Chung Leong

          #5
          Re: Test for the existence of a null property

          wildernesscat@g mail.com wrote:
          Hello there,
          >
          I'm looking for a method to test, whether an object has a certain
          property.
          Consider the following snippet:
          >
          class A { var $aaa; }
          $var = new A;
          >
          (Assuming that the structure of class A is unknown) I need a way to
          check whether $var->aaa exists (test positive), and whether $var->xxx
          exists (test negative). I tried boolean tests, isset(), is_null(), but
          they can't tell the difference.
          I guess I could convert the object to an array and test its indices,
          but that wouldn't be practical for large object with many fields.
          >
          Any other ideas?
          Psst! You can use array_key_exist s('aaa', $var) to test for the
          existence of a property. I didn't tell you that by the way.

          Comment

          • Jerry Stuckle

            #6
            Re: Test for the existence of a null property

            wildernesscat wrote:
            Hi Jerry,
            >
            I'm trying to write a function that checks whether a given object is of
            a known class (named 'A' in this example). I want it to work even if
            the object has been constructed manually, starting from 'new
            StdClass()'.
            >
            Danny
            >
            >
            Yes, that's a lot different than your original question.

            You can use is_a() as Marcin suggested. But what's to stop me from
            creating my own class 'A'?

            The real question is - why do you need this "feature"? It violates
            several principles of OO.

            OO principles include that you don't care WHAT the object is - all you
            care about is its interface (functions). If you need to depend on a
            specific class, you need to rethink your design.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • wildernesscat

              #7
              Re: Test for the existence of a null property

              Hi Marcin,
              >From my experience, the is_a() function does not work unless the object
              has been created explicitly using its class name (Foo in your example).
              If the object has been created via StdClass, is_a() doesn't recognize
              it.

              As for using get_class_vars( ), I'm getting into O(n2) here. You
              suggested that I scan the properties of one object and compare them to
              the other one, right? I was looking for a O(n) solution.

              Regards,
              Danny
              I think this is VERY different from what you asked in the first mail.
              But to do that, you want something like this:
              >
              class Foo (
              function Foo() {}
              }
              >
              $f = new Foo();
              ...
              if (is_a($f, 'Foo')) {
              echo "yes";
              }
              ...
              echo "\$f is of type: " . get_class($f);
              >
              To do what you asked before (and hence break some main concepts in
              OO), you can:
              >
              $vars = get_class_vars( 'Foo');

              Comment

              • wildernesscat

                #8
                Re: Test for the existence of a null property

                That's interesting. I didn't know that array_key_exist s() works on
                objects, but someone suggested I used property_exists (), and that seems
                to solve the riddle.
                Psst! You can use array_key_exist s('aaa', $var) to test for the
                existence of a property. I didn't tell you that by the way.

                Comment

                • wildernesscat

                  #9
                  Re: Test for the existence of a null property

                  Hi Jerry,
                  You can use is_a() as Marcin suggested. But what's to stop me from
                  creating my own class 'A'?
                  As I told Marcin, is_a() will not be of much use here. I need an
                  in-depth comparison, and not just resemblance in names.
                  The real question is - why do you need this "feature"? It violates
                  several principles of OO.
                  Actually, I don't think that I violate any major OO principles. This is
                  almost like asking an object whether it supports certain interfaces.
                  I'll pinpoint my question. I want to know whether a certain object has
                  the _public_ properties of a given class. Does that make sense? Suppose
                  I have an object that has public properties $aaa, $bbb, and $ccc - I'd
                  like to know whether class A also has them.
                  OO principles include that you don't care WHAT the object is - all you
                  care about is its interface (functions). If you need to depend on a
                  specific class, you need to rethink your design.
                  >
                  Regards,
                  Danny

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Test for the existence of a null property

                    wildernesscat wrote:
                    Hi Jerry,
                    >
                    >
                    >>You can use is_a() as Marcin suggested. But what's to stop me from
                    >>creating my own class 'A'?
                    >
                    >
                    As I told Marcin, is_a() will not be of much use here. I need an
                    in-depth comparison, and not just resemblance in names.
                    >
                    >
                    >>The real question is - why do you need this "feature"? It violates
                    >>several principles of OO.
                    >
                    >
                    Actually, I don't think that I violate any major OO principles. This is
                    almost like asking an object whether it supports certain interfaces.
                    I'll pinpoint my question. I want to know whether a certain object has
                    the _public_ properties of a given class. Does that make sense? Suppose
                    I have an object that has public properties $aaa, $bbb, and $ccc - I'd
                    like to know whether class A also has them.
                    >
                    >
                    >>OO principles include that you don't care WHAT the object is - all you
                    >>care about is its interface (functions). If you need to depend on a
                    >>specific class, you need to rethink your design.
                    >>
                    >
                    >
                    Regards,
                    Danny
                    >
                    Danny,

                    Again, that's a completely different question.

                    No, there's nothing wrong to determine of a class supports specific
                    properties. But that's not the same as expecting a specific class.

                    And in many cases classes shouldn't have public properties - they should
                    have private properties and public access methods. It isolates the
                    interface from the implementation.

                    I see a lot of public properties in PHP code - mainly because before
                    PHP5 you couldn't define private properties. However, if you look at
                    C++, Java and Smalltalk, you'll find almost no public properties - only
                    methods.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    Working...