Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2013, 05:02 PM   PM User | #1
paperino00
New Coder

 
Join Date: Nov 2011
Posts: 92
Thanks: 2
Thanked 0 Times in 0 Posts
paperino00 is an unknown quantity at this point
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..
paperino00 is offline   Reply With Quote
Old 01-10-2013, 05:10 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-10-2013, 05:12 PM   PM User | #3
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
-> 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
TFlan is offline   Reply With Quote
Old 01-10-2013, 05:21 PM   PM User | #4
paperino00
New Coder

 
Join Date: Nov 2011
Posts: 92
Thanks: 2
Thanked 0 Times in 0 Posts
paperino00 is an unknown quantity at this point
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;
        }
    }

paperino00 is offline   Reply With Quote
Old 01-10-2013, 05:24 PM   PM User | #5
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
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
TFlan is offline   Reply With Quote
Old 01-10-2013, 05:25 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You keep modifying more information into your post :/
Quote:
$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.
Fou-Lu is offline   Reply With Quote
Old 01-10-2013, 05:34 PM   PM User | #7
paperino00
New Coder

 
Join Date: Nov 2011
Posts: 92
Thanks: 2
Thanked 0 Times in 0 Posts
paperino00 is an unknown quantity at this point
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 ?
paperino00 is offline   Reply With Quote
Old 01-10-2013, 07:03 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:28 PM.


Advertisement
Log in to turn off these ads.