Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    97
    Thanks
    2
    Thanked 0 Times in 0 Posts

    scope :: of class

    Hello, I read a code when a function inside a class is called by :: instead of ->, why ? it isn't a const, static, etc function...

    PHP Code:
    class search
    {
     function 
    init_search($type)
      {
         
    //function code ...
      
    }
    }
    $search search::init_search($type);
    global ${
    $obj}; // why is $ used twice ? 
    $this->search->db_tbl "video"//what does that mean ? 
    Last edited by paperino00; 01-10-2013 at 05:16 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That would violate E_STRICT. It works so long as $this isn't referred to merely to support backwards compatibility with PHP4 objects. So if you open error reporting all the way (E_STRICT may need to be |'d into it if you have a < 5.3 I think it is), it will report:
    Code:
    Strict Standards: Non-static method search::init_search() should not be called statically
    In other words, the code is either old and written for pre-5.0 PHP OO, or it was just poorly written. It should be using the static modifier on the function signature.

  • #3
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    -> is used when referring to a member of an object.

    :: is the Scope Resolution Operator and is used to refer to a static member of a class.

    This will answer your variable question as well

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    97
    Thanks
    2
    Thanked 0 Times in 0 Posts
    that's all the class code, so why does it work ?

    PHP Code:
    class search {
    function 
    init_search($type='video')
        {
            global 
    $Cb;
            if(
    $Cb->search_types[$type])
            {
                
    $obj $Cb->search_types[$type];
                global ${
    $obj};
                ${
    $obj}->init_search();
                return ${
    $obj}->search;
            }else
            {
                global 
    $cbv;
                
    $cbv->init_search();
                return 
    $cbv->search;
            }
        }


  • #5
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    PHP Code:
    class search {
        function 
    init_search($type='video'){
            global 
    $Cb;
            if(
    $Cb->search_types[$type]){
                
    $obj $Cb->search_types[$type];
                global ${
    $obj};
                ${
    $obj}->init_search();
                return ${
    $obj}->search;
            }else{
                global 
    $cbv;
                
    $cbv->init_search();
                return 
    $cbv->search;
            }
        }

    That in itself works fine.


    Calling it using
    PHP Code:
    $search search::init_search($type
    Will NOT work because of what Fou-Lou said

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You keep modifying more information into your post :/
    $this->search->db_tbl = "video"; //what does that mean ?
    That would set 'this' object's member property 'search''s member property 'db_tbl' to the value of 'video'.
    I don't like it since you are directly accessing an object's property. PHP is datatype weak, so I could set db_tbl to new stdclass which would likely break the entire application in use. Always use setters especially in PHP to enforce proper values expected within a member.

    Also, a side add, the :: is also used for super scoping. These can be done on parent::, self:: and static::. I think its just the three in PHP.

    Edit:
    Also, that will work above since there is no explicit reference to $this. It will violate strict standards though.
    You should also eliminate the use of global. Its bad enough in procedural code, but in OO it becomes an absolute nightmare. Reserve it purely for work in functions that have set signatures that cannot be modified. Opt to pass in object $Cb instead, which can also be type hinted.

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    97
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You keep modifying more information into your post :/

    That would set 'this' object's member property 'search''s member property 'db_tbl' to the value of 'video'.
    could you write a small example about it ?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    class T
    {
        public 
    $search;
        public function 
    aFunc()
        {
            
    $this->search->db_tbl 'value';
        }
    }

    class 
    S
    {
        public 
    $db_tbl;
    }

    $t = new T();
    $t->search = new S();
    $t->aFunc(); 
    Of course the problem with these being public is I can also do as such:
    PHP Code:
    $t->search 'cat'
    In which case $t->search->db_tbl will now throw a warning. If that were a method call, it would then throw an fatal error.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •