TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   class...method question... (http://www.talkphp.com/absolute-beginners/4154-class-method-question.html)

allworknoplay 04-17-2009 11:07 PM

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?

Salathe 04-17-2009 11:13 PM

$this references the calling object, and fullName a property of that object. Basic introduction: http://php.net/language.oop5.basic

allworknoplay 04-18-2009 03:28 AM

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

Kalle 04-18-2009 07:43 AM

Quote:

Originally Posted by allworknoplay (Post 23176)
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 ;)

Tanax 04-18-2009 12:48 PM

Quote:

Originally Posted by allworknoplay (Post 23176)
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.

allworknoplay 04-18-2009 02:00 PM

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".

Salathe 04-18-2009 02:45 PM

Quote:

Originally Posted by allworknoplay (Post 23187)
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.

allworknoplay 04-18-2009 02:51 PM

Quote:

Originally Posted by Salathe (Post 23190)
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 04-18-2009 04:11 PM

Quote:

Originally Posted by Tanax (Post 23184)

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?

Kalle 04-18-2009 04:46 PM

Quote:

Originally Posted by allworknoplay (Post 23187)
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/

;)

Tanax 04-18-2009 06:30 PM

Quote:

Originally Posted by allworknoplay (Post 23195)
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']; 



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