 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
04-17-2009, 11:07 PM
|
#1 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
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) {
$this->fullName = $name . " Smith";
return $this->fullName;
}
But if I do this, I think I would get back
"John Smith" too...
Code:
public function printName($name) {
$fullName = $name . " Smith";
return $fullName;
}
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?
|
|
|
|
04-17-2009, 11:13 PM
|
#2 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
$this references the calling object, and fullName a property of that object. Basic introduction: http://php.net/language.oop5.basic
|
|
|
|
04-18-2009, 03:28 AM
|
#3 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
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() {
//DO SOMETHING HERE AND RETURN DATA
$viewPageEntry[] = $this->starting_no;
$viewPageEntry[] = $this->end_count;
return $viewPageEntry;
}
In my HTML I am calling it and then assigning it to a variable,
then accessing each data this way.
Code:
$getViewPage = $pagination->viewPage();
echo $getViewPage[0];
echo $getViewPage[1];
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);
echo $pagination->viewPage(end_count);
Maybe I should access it statically? Would that work?
Code:
$getViewPage::starting_no
$getViewPage::end_count
I get this error when I try the above code:
Quote:
|
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
|
|
|
|
|
04-18-2009, 07:43 AM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
Quote:
Originally Posted by allworknoplay
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() {
//DO SOMETHING HERE AND RETURN DATA
$viewPageEntry[] = $this->starting_no;
$viewPageEntry[] = $this->end_count;
return $viewPageEntry;
}
In my HTML I am calling it and then assigning it to a variable,
then accessing each data this way.
Code:
$getViewPage = $pagination->viewPage();
echo $getViewPage[0];
echo $getViewPage[1];
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);
echo $pagination->viewPage(end_count);
Maybe I should access it statically? Would that work?
Code:
$getViewPage::starting_no
$getViewPage::end_count
I get this error when I try the above code:
|
Paamayim Nekudotayim means "Double colon" in hebrew.
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:
echo ClassName::$property;
The following will call a constant:
PHP Code:
echo ClassName::CONSTANT;
or inside a class you can use self:: and parent:: instead of the class name if you are within a method scope of the class.
In PHP 5.3 the following is possible:
PHP Code:
class A
{
public static $b;
public function __construct()
{
/* Sets A::$b staticlly */
$this::$b = 'ABC';
}
}
$a = new A;
echo '$a::$b: ' . $a::$b . PHP_EOL;
echo 'A::$b: ' . A::$b . PHP_EOL;
It will print:
Code:
$a::$b: ABC
A::$b: ABC
Because the dynamic syntax is possible here ;)
__________________
|
|
|
|
The Following User Says Thank You to Kalle For This Useful Post:
|
|
04-18-2009, 12:48 PM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by allworknoplay
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() {
//DO SOMETHING HERE AND RETURN DATA
$viewPageEntry[] = $this->starting_no;
$viewPageEntry[] = $this->end_count;
return $viewPageEntry;
}
In my HTML I am calling it and then assigning it to a variable,
then accessing each data this way.
Code:
$getViewPage = $pagination->viewPage();
echo $getViewPage[0];
echo $getViewPage[1];
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);
echo $pagination->viewPage(end_count);
Maybe I should access it statically? Would that work?
Code:
$getViewPage::starting_no
$getViewPage::end_count
I get this error when I try the above code:
|
Yah, remember that the code I used for that pagination class I wrote is quite old. Anyhow, to the function, I just returned an array because I felt it would be easier.
You however, could create 2 functions for it.
PHP Code:
$starting_no = $pagination->getStart();
$end_cound = $pagination->getEnd();
Surely you can do them staticly, but remember you need to set the class variables to public then - which I strongly advice you not to do, 'cause then you can also accidently change the value aswell Classname::$variable = "bogus" which, needless to say, is quite a security issue.
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.
__________________
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
04-18-2009, 02:00 PM
|
#6 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
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".
|
|
|
|
04-18-2009, 02:45 PM
|
#7 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by allworknoplay
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".
|
To access the property directly ( $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.
|
|
|
|
04-18-2009, 02:51 PM
|
#8 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Salathe
To access the property directly ($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.
|
Thanks Salathe,
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?
|
|
|
|
04-18-2009, 04:11 PM
|
#9 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Tanax
Surely you can do them staticly, but remember you need to set the class variables to public then - which I strongly advice you not to do, 'cause then you can also accidently change the value aswell Classname::$variable = "bogus" which, needless to say, is quite a security issue.
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.
|
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?
|
|
|
|
04-18-2009, 04:46 PM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
Quote:
Originally Posted by allworknoplay
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".
|
FYI, PHP 5.3 is not released as final yet, but you can grab the RC1 and play around with it:
http://qa.php.net/
or grab a snapshot:
http://snaps.php.net/
And Windows:
http://windows.php.net/
;)
__________________
|
|
|
04-18-2009, 06:30 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by allworknoplay
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?
|
I would say never, but there are obviously situations when you want to use public. The only reason I say it's a security issue is due to the fact that you can edit it aswell as getting the value from it.
If you want to access a variable directly, I would make use of the __get method in PHP5.
PHP Code:
private $variable = array();
public function __get($name) {
if(array_key_exists($name, $variable) {
return $this->variable[$name];
}
}
//use:
$pagination->no_result; //would get value of $variable['no_result'];
__________________
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|