Verdagon;10900536 wrote:

With your code I get this error: PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /Users/Verdagon/Desktop/perello/server/date.php on line 26

Yeah, that was my point.. so when I try:

echo new dateTime()->getOffset();

I get the same problem as you.. but as I just mentioned, I do get results with:

$d = new dateTime();
echo $d->getOffset();

EDIT - So I am guessing you cannot echo out a new class without creating an object first (I've never tried it before coming across this thread). But it doesn't explain why you cannot create an object and echo it out that way..

    A mystery, isn't it? But it's bigger than that. It's not just echo that won't do this, its anything. Just writing this gives me an error:

    <?php
    	new bear->sleep();
    ?>

    PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /Users/Verdagon/Desktop/perello/server/date.php on line 2

    In java, we can write new Bear().sleep() and it will work just fine. I wonder why PHP can't do that? I run into this all the time in my php code... I don't like making unnecessary variables, y'know?

      It is possible that PHP evaluates dateTime()->getOffset() before it evaluates new.

      I don't have PHP installed, making it hard to test. But if that is the case this should work, forcing PHP to create a new object first:

      echo (new dateTime())->getOffset(); 
      

        I think you need to create an object first, as in your $d=new dataTime()...
        I don't think you can simply echo out a class in itself as we have been trying (again.. I never even thought of trying that until this thread).. so I am more concerned with why, even when you create an object ($d) and try and echo out the results from the $d->getOffset(); it doesn't work for you.

        This is why I am asking which version of PHP is your server using (it might not be an issue.. I don't know.. Im not sure how old this default dateTime class is with regards to PHP versions)..

        As for Java, well, PHP isn't quite Java..

          Piranha;10900542 wrote:

          It is possible that PHP evaluates dateTime()->getOffset() before it evaluates new.

          I don't have PHP installed, making it hard to test. But if that is the case this should work, forcing PHP to create a new object first:

          echo (new dateTime())->getOffset(); 
          

          I tried your suggestion, Piranha.. same error I'm afraid.

            Oh no, I must have been unclear.

            $d = new DateTime();
            echo $d->getOffset();

            this DOES work. I was merely wondering why php will run that but won't run it when it's all in one line such as new DateTime()->getOffset()

            I tried what you suggested, Piranha, but no luck. It gives the same error.

              Verdagon;10900545 wrote:

              Oh no, I must have been unclear.

              $d = new DateTime();
              echo $d->getOffset();

              this DOES work. I was merely wondering why php will run that but won't run it when it's all in one line such as new DateTime()->getOffset()

              Ooooohhhhh hehe... My bad.. I misunderstood..well then, that solves everything 😉
              I guess you cannot echo out a class in itself...

                nrg_alpha's post above has it right: new Anything()->aMethod() is just plain bad syntax.

                  Well, I'd say it's bad syntax according to the current PHP, but maybe it's something that should be changed in future PHPs?

                  I mean, new DateTime()->getOffset() makes perfect sense to me...

                    I believe it is because new returns an actual object, not a reference to an object. Method chaining only works with references (which is the default mode in PHP5 for copying existing objects). Whether something should be changed to allow what you want to do is a theoretical question for which I have no idea what should or should not be the "correct" answer in terms of OOP theory.

                      Sounds like something worthy of a blog article...

                        NogDog wrote:

                        I believe it is because new returns an actual object, not a reference to an object.

                        I do not think so. As of PHP 5 new returns a reference (as in pointer) to the newly created object.

                          OK, then I don't know why, just that apparently you cannot method chain with new. 🙂

                          If it really bothers you and it's something you want to do multiple times, you can do this: 😉

                          class myDateTime
                          {
                             public static function get()
                             {
                                return new DateTime();
                             }
                          }
                          
                          echo myDateTime::get()->getOffset();
                          

                            Or here's a (possibly silly) idea to make the entire class "static":

                            class myDateTime
                            {
                               public static function get($attr)
                               {
                                  $dt = new DateTime();
                                  if(method_exists($dt, $attr))
                                  {
                                     return $dt->$attr();
                                  }
                                  elseif(property_exists($dt, $attr))
                                  {
                                     return $dt->$attr;
                                  }
                                  else
                                  {
                                     throw new Exception("Invalid attribute '$attr'");
                                  }
                               }
                            }
                            
                            echo myDateTime::get('getOffset');
                            

                            :rolleyes:

                              Oh wow, that actually worked! Thank you, I will use this (your first suggestion, with the factory method) religiously =)

                                Write a Reply...