![]() |
class...method question...
Ok so let's say I have a method like so....
And I pass it the first name "John". I should get back "John Smith". Code:
public function printName($name) {"John Smith" too... Code:
public function printName($name) {So my question is, what is the significance of "$this->fullName" in the first example? I know that it is acting as a reference (pointer) to the name "John Smith" that you are assigning it to. And that "$this->fullName" can be used locally within the method. Is it ONLY local to the method? Or can other method's use it? I guess I am missing the point of assigning a variable to "$this->fullName" when you can just create a variable called "$fullname" and return that? |
$this references the calling object, and fullName a property of that object. Basic introduction: http://php.net/language.oop5.basic |
Salathe/Tanax & company:
I currently have a method that needs to return 2 variables...maybe even 3, but let's say 2 for now. I'm currently doing it this way, do you know of any better way of doing it? Tanax, this might look familiar to you.... Code:
function viewPage() {then accessing each data this way. Code:
$getViewPage = $pagination->viewPage();I thought maybe OO was a bit more powerful or had a better way to access its data? I thought maybe I would be able to access the data this way but it doesn't work. Code:
echo $pagination->viewPage(starting_no);Code:
$getViewPage::starting_noQuote:
|
Quote:
You cannot call a property like that in versions prior to PHP 5.3, in 5.3 and newer it will call the constants named 'starting_no' and 'end_count'. To call a property staticlly you need to call it with a class name: PHP Code:
PHP Code:
In PHP 5.3 the following is possible: PHP Code:
Code:
$a::$b: ABC |
Quote:
You however, could create 2 functions for it. PHP Code:
It's safer to have a method instead to fetch a private classvariable, that way you can be sure that it's only able to return the current value, and never change it. |
Thanks Kalle/Tanax:
My PHP version is 5.28, so looks like I should upgrade. I think I'm starting to get the hang of OO. The static way of accessing properties (self::,parent::) is very interesting, I want to try to incorporate that into my class somehow, hopefully for a good reason, but if not, at least to get a good understanding of how they work just by using it. Do I have to declare the property as "public" in order to access it outside of the class? Right now all my properties are set to "private". |
Quote:
$instance->property), yes it needs to be public. Also note that with private, any classes that extend the class will not have access to it. If you want extending classes to be able to 'see' the private property then it will need to instead be protected. |
Quote:
I think I am a long ways before I write another class extending another class. haha...but I understand. public - anyone from anywhere can access the property private - only accessible within the class protected - only accessible within the class and child classes that have been extended... And the rules work exactly the same way for methods too? Not just properties....right? |
Quote:
When/what is the appropriate time to ever set a property to "public" or is it never? I like the idea of accessing a property value directly, it seems cleaner....or shorter than to have to create a "getter" method to get obtain the data value. I can also see the security issue side of it, so I guess my question is: 1) why would PHP implement this if there are security issues? 2) is there ever an appropriate time set a property to public? |
Quote:
http://qa.php.net/ or grab a snapshot: http://snaps.php.net/ And Windows: http://windows.php.net/ ;) |
Quote:
If you want to access a variable directly, I would make use of the __get method in PHP5. PHP Code:
|
| All times are GMT. The time now is 12:35 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0