classes$function

Discussion in 'PHP' started by fluteofliar, Jul 2, 2010.

  1. #1
    In php,AFAIK dollarsign($) is part of variable thatswhy we always variable
    using this sign........
    Now,whats wrong when i use the above defination while accessing a member variable of a class using object, for instance
    <?php
    class hello
    {
    public $a="php seems sometime illogical";
    function _construct()
    {
    echo "howz your php experience?";
    }
    }
    $obj=new hello();
    $obj->a;//this works fine
    $ob->$a;//error::can,t acess empty property?

    2)does function are define at the point they call in php?
    why its necessary to call inner function first before outer function in nested function?:confused:
     
    fluteofliar, Jul 2, 2010 IP
  2. dipenrique

    dipenrique Peon

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $ob->$a;

    these are needed just for static variables ! if your variable is not static you dont need a $ sign before 'a'.


    and i cant answer 2nd question because I am also just a beginner in php
     
    dipenrique, Jul 2, 2010 IP
  3. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thankz mate :)
    but here omitting dollar sign become compulsary since i am getting error..
    and why its showing different behaviour with static variables?
     
    fluteofliar, Jul 2, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Correct.

    When you use $obj->a you are accessing the PROPERTY of the object as referenced inside the object as $a.....

    In the latter, however, you are passing in a VARIABLE as the PROPERTY. Consider this.

    
    <?php
    class hello
    {
    public $a="world";
    public $world="hello";
    function _construct()
      {
      echo "howz your php experience?";
      }
    }
    
    $obj=new hello();
    $a = $obj->a;
    print $obj->$a;
    
    PHP:
    Output: hello

    This is because I have passed in the value of $a (world) and accessed $obj->world because of it (and this is why it is correctly reporting that its an empty property)

    Im not sure i understand what you are asking there?
     
    lukeg32, Jul 2, 2010 IP
  5. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    lukeg32 this defination clear my concept...thankz mate

    well i try to make my 2nd ques more understandable
    <?php
    function foo()
    {
    function bar()
    {
    echo "you have to first call foo()";
    }
    }
    
    PHP:
    in these nested function why we need to call first foo() and then only we can call
    bar()?
     
    fluteofliar, Jul 2, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Ahhh right, I understand you now.

    Yes, the inner function will only be accessible once you call the outer function for the first time; I dont think that functionality is terribly useful and you should also bear in mind that if you call the outer function more than once, it will throw an error about not redeclaring the (inner) function
     
    lukeg32, Jul 2, 2010 IP
  7. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i think if we call outer function more than once it will throw an error for declaring the inner function more than once...i mean whenver we call outer function
    inner function get declare and may be thats why first we need to call outer function....i may be wrong
     
    fluteofliar, Jul 2, 2010 IP
  8. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #8
    You can call the inner function as often as you like; its only when you call the outer function it will error, since it is trying to redeclare the function.... E.G

    
    <?php
    function foo()
      {
      function bar()
        {
        print "you have to first call foo()";
        }
      }
    
    print bar(); # This will error as its undefined
    print foo(); # This will be fine (no output)
    print bar(); # This will be fine
    print bar(); # This will be fine
    print foo(); # This will error as redeclared function
    ?>
    
    
    PHP:
     
    lukeg32, Jul 2, 2010 IP
  9. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    well i come from c++ ground..
    so in c++ i read that we can declare a variable or function as many time we want with maintaining one defination rule.
    is there any difference in declaration and defination in php?if there is then plz let me know
     
    Last edited: Jul 2, 2010
    fluteofliar, Jul 2, 2010 IP
  10. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Im not sure i follow again... sorry, maybe its been a long day - can you give another example? :)

    PHP is a higher level than C so there are similarities.... indeed, there are compilers that will convert it to C->binary .... but i cant answer your specific question until I understand it :/
     
    lukeg32, Jul 2, 2010 IP
  11. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    In c++,
    int sum(int a, int b);//since this is declaration , i can write this line several times in my program..
    int sum(int a, int b){}//this is defination,i can,t write defination of one function more than once else it ll lead to linker error
    means in c/c++ declaration determine scope,type,lifetime of variable while defination determine the memory required by variable
    in php,
    its giving error on redeclaration , as you said
    why its so?
     
    fluteofliar, Jul 2, 2010 IP
  12. fluteofliar

    fluteofliar Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    so is there any difference between declaration and defination in php?
     
    fluteofliar, Jul 3, 2010 IP