Ninjakreborn Posted September 7, 2007 Share Posted September 7, 2007 What does :: mean in OOP. Quote Link to comment https://forums.phpfreaks.com/topic/68373-solved-the-symbol/ Share on other sites More sharing options...
Illusion Posted September 7, 2007 Share Posted September 7, 2007 as per my knowledge, u can access variables and members of a class with the :: operator with out creating object for that. Quote Link to comment https://forums.phpfreaks.com/topic/68373-solved-the-symbol/#findComment-343769 Share on other sites More sharing options...
Ninjakreborn Posted September 7, 2007 Author Share Posted September 7, 2007 So in essence if I have one class that I instantiate. I can then make a call to a function within another class without ever having to instantiate it. That is what I understood based on what you said, is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/68373-solved-the-symbol/#findComment-343772 Share on other sites More sharing options...
hostfreak Posted September 7, 2007 Share Posted September 7, 2007 For the most part yes. Have you checked it out in the manual? http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php Quote Link to comment https://forums.phpfreaks.com/topic/68373-solved-the-symbol/#findComment-343775 Share on other sites More sharing options...
Ninjakreborn Posted September 7, 2007 Author Share Posted September 7, 2007 Thank you, I was looking for that but had troubles locating it. This will answer my question in detail, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/68373-solved-the-symbol/#findComment-343776 Share on other sites More sharing options...
roopurt18 Posted September 7, 2007 Share Posted September 7, 2007 You use it to access class functions and members statically, where statically means you don't have an instance of the object. However, you have to be careful how you use it. If your class is designed correctly in the first place, you shouldn't be directly setting or reading the values of class properties (variables). These should only be set and read through accessor functions. For example: <?php class a { public $property = null; // Rest of class follows... } $var = new a(); // The next two lines of code are pretty bad practice IMO $a->property = "Hello, World!" echo $a->property; ?> Realistically, the member $property should be private or protected and only settable / readable through methods (functions). Now, let's change our definition of the class slightly above. <?php class a { public static $static_value = null; } // We can access the static value like so a::$static_value = "Hi there!"; echo a::$static_value; ?> (edit) Doh! PHP Does support consts! (/edit) The same examples can be applied to functions within a class definition. You can call a function within a class using the static syntax: my_class::my_function(). You have to be careful using this with class functions though; if the function uses the $this keyword it will not be defined and your code will crash. The reason $this is not defined is simple: $this refers to the instance of the class but you're calling the function without an instance, so there is no $this to refer to! IMO, the best use of calling functions in this manner in PHP is to simulate namespaces, which is a short way of saying it allows you to avoid naming collisions between functions that perform similar functions on different types of data. Quote Link to comment https://forums.phpfreaks.com/topic/68373-solved-the-symbol/#findComment-343783 Share on other sites More sharing options...
Ninjakreborn Posted September 7, 2007 Author Share Posted September 7, 2007 Ah thank you for the additional advice. I am going to copy your code samples listed here for reference. Thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/68373-solved-the-symbol/#findComment-343793 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.