TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-17-2009, 11:07 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default 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?
allworknoplay is offline  
Reply With Quote
Old 04-17-2009, 11:13 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

$this references the calling object, and fullName a property of that object. Basic introduction: http://php.net/language.oop5.basic
Salathe is offline  
Reply With Quote
Old 04-18-2009, 03:28 AM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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
allworknoplay is offline  
Reply With Quote
Old 04-18-2009, 07:43 AM   #4 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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 ;)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
allworknoplay (04-18-2009)
Old 04-18-2009, 12:48 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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.
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
allworknoplay (04-18-2009)
Old 04-18-2009, 02:00 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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".
allworknoplay is offline  
Reply With Quote
Old 04-18-2009, 02:45 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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.
Salathe is offline  
Reply With Quote
Old 04-18-2009, 02:51 PM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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?
allworknoplay is offline  
Reply With Quote
Old 04-18-2009, 04:11 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post

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?
allworknoplay is offline  
Reply With Quote
Old 04-18-2009, 04:46 PM   #10 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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/

;)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 04-18-2009, 06:30 PM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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']; 
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract Classes sketchMedia Advanced PHP Programming 18 02-28-2013 05:38 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
A nice way to use a MySQL class Gibou Advanced PHP Programming 0 11-27-2007 11:42 PM


All times are GMT. The time now is 05:25 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design