Capitalization and Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Good Man

    Capitalization and Classes

    Hi there

    I'm learnin' myself some OO programming (finally), and was wondering if
    someone could briefly tell me why when setting up my object...

    $p = new Person();

    .... that the following both print "Bob" to the screen:

    print $p->name;
    print $p->Name;


    The method in the class is getName, so why would "name" (lowercase) work
    here? All my experience in PHP says that uppercase/lowercase is vitally
    important!

    Here's the class:


    class Person {

    function __get($property ) {
    $method = "get{$property} ";
    if(method_exist s($this,$method )) {
    return $this->$method();
    }
    }

    function getName() {
    return "Bob";
    }

    function getAge() {
    return 44;
    }

    }

  • Michael Fesser

    #2
    Re: Capitalization and Classes

    ..oO(Good Man)
    >I'm learnin' myself some OO programming (finally), and was wondering if
    >someone could briefly tell me why when setting up my object...
    >
    >$p = new Person();
    >
    >... that the following both print "Bob" to the screen:
    >
    >print $p->name;
    >print $p->Name;
    >
    >
    >The method in the class is getName, so why would "name" (lowercase) work
    >here? All my experience in PHP says that uppercase/lowercase is vitally
    >important!
    Variable names are case-sensitive, method names are not.

    Micha

    Comment

    • Good Man

      #3
      Re: Capitalization and Classes

      Michael Fesser <netizen@gmx.de wrote in
      news:0tg1j3d60a 4ut617q8c0g87fj nqsta9hmb@4ax.c om:
      .oO(Good Man)
      >
      >>I'm learnin' myself some OO programming (finally), and was wondering if
      >>someone could briefly tell me why when setting up my object...
      >>
      >>$p = new Person();
      >>
      >>... that the following both print "Bob" to the screen:
      >>
      >>print $p->name;
      >>print $p->Name;
      >>
      >>
      >>The method in the class is getName, so why would "name" (lowercase) work
      >>here? All my experience in PHP says that uppercase/lowercase is vitally
      >>important!
      >
      Variable names are case-sensitive, method names are not.
      Many thanks.

      Comment

      • Darko

        #4
        Re: Capitalization and Classes

        On Nov 6, 8:37 pm, Good Man <he...@letsgo.c omwrote:
        Hi there
        >
        I'm learnin' myself some OO programming (finally), and was wondering if
        someone could briefly tell me why when setting up my object...
        >
        $p = new Person();
        >
        ... that the following both print "Bob" to the screen:
        >
        print $p->name;
        print $p->Name;
        >
        The method in the class is getName, so why would "name" (lowercase) work
        here? All my experience in PHP says that uppercase/lowercase is vitally
        important!
        >
        Here's the class:
        >
        class Person {
        >
        function __get($property ) {
        $method = "get{$property} ";
        if(method_exist s($this,$method )) {
        return $this->$method();
        }
        }
        >
        function getName() {
        return "Bob";
        }
        >
        function getAge() {
        return 44;
        }
        >
        }
        It's impossible that works. You probably meant $p->getname() and $p-
        >getName()

        Comment

        • Good Man

          #5
          Re: Capitalization and Classes

          Darko <darko.maksimov ic@gmail.comwro te in
          news:1194378798 .505407.6390@k7 9g2000hse.googl egroups.com:

          >print $p->name;
          >print $p->Name;
          >>
          >The method in the class is getName, so why would "name" (lowercase)
          >work here? All my experience in PHP says that uppercase/lowercase is
          >vitally important!
          >>
          >Here's the class:
          >>
          >class Person {
          >>
          > function __get($property ) {
          > $method = "get{$property} ";
          > if(method_exist s($this,$method )) {
          > return $this->$method();
          > }
          > }
          >>
          > function getName() {
          > return "Bob";
          > }
          >>
          > function getAge() {
          > return 44;
          > }
          >>
          >}
          >
          It's impossible that works. You probably meant $p->getname() and $p-
          >>getName()

          Promise you that it does :)

          It's all about the __get($property ) Interceptor Method :)



          Comment

          • Darko

            #6
            Re: Capitalization and Classes

            On Nov 6, 9:11 pm, Good Man <he...@letsgo.c omwrote:
            Darko <darko.maksimo. ..@gmail.comwro te innews:11943787 98.505407.6390@ k79g2000hse.goo glegroups.com:
            >
            >
            >
            print $p->name;
            print $p->Name;
            >
            The method in the class is getName, so why would "name" (lowercase)
            work here? All my experience in PHP says that uppercase/lowercase is
            vitally important!
            >
            Here's the class:
            >
            class Person {
            >
            function __get($property ) {
            $method = "get{$property} ";
            if(method_exist s($this,$method )) {
            return $this->$method();
            }
            }
            >
            function getName() {
            return "Bob";
            }
            >
            function getAge() {
            return 44;
            }
            >
            }
            >
            It's impossible that works. You probably meant $p->getname() and $p-
            >getName()
            >
            Promise you that it does :)
            >
            It's all about the __get($property ) Interceptor Method :)
            Right! Those fancy new methods I chose not to read when reading the
            modern php manual. Knew I would regret! :)

            Comment

            • Steve

              #7
              Re: Capitalization and Classes


              "Darko" <darko.maksimov ic@gmail.comwro te in message
              news:1194396610 .447598.279570@ 22g2000hsm.goog legroups.com...
              On Nov 6, 9:11 pm, Good Man <he...@letsgo.c omwrote:
              >Darko <darko.maksimo. ..@gmail.comwro te
              >innews:1194378 798.505407.6390 @k79g2000hse.go oglegroups.com:
              >>
              >>
              >>
              >print $p->name;
              >print $p->Name;
              >>
              >The method in the class is getName, so why would "name" (lowercase)
              >work here? All my experience in PHP says that uppercase/lowercase is
              >vitally important!
              >>
              >Here's the class:
              >>
              >class Person {
              >>
              > function __get($property ) {
              > $method = "get{$property} ";
              > if(method_exist s($this,$method )) {
              > return $this->$method();
              > }
              > }
              >>
              > function getName() {
              > return "Bob";
              > }
              >>
              > function getAge() {
              > return 44;
              > }
              >>
              >}
              >>
              It's impossible that works. You probably meant $p->getname() and $p-
              >>getName()
              >>
              >Promise you that it does :)
              >>
              >It's all about the __get($property ) Interceptor Method :)
              more specifically:

              $method = "get{$property} ";

              but, this is a pretty hoaky way of hacking php to act like it has built-in
              getters/setters. __get only ever gets called when php CAN'T determine the
              interface name...NOT when you call a defined interface like getName(). this
              method predicates that all developers programming this class knows the magic
              and that the (readable) interfaces they create begin with 'get' and that
              they take NO parameters. this not only effects the developer but the caller
              as well. the caller must know that $obj->foo('param') will fail to do
              anything since the magic code above will die a painful death if 'param' is
              required and not optional...via $this->$method();. consider that you too had
              to initially think 'why in the hell would that work and where does Name come
              from'. you do NOT want people asking those questions when looking/using your
              code, especially when it's another developer...or your boss.

              better to be explicit in object architecture and consumption. until php
              formally provides built-in getters/setters for interfaces THAT ARE DEFINED,
              i'd consider it an entire waste of time trying to fudge it using
              __get/__set. stay away from voodoo.


              Comment

              Working...