PDA

View Full Version : -> Huh?



jscheuer1
10-11-2009, 08:00 AM
What does -> mean in PHP?

And, what sort of data types can be on the left side of it and what sort on the right?

What does it do, please give a couple real basic examples.

gurmeet
10-11-2009, 08:20 AM
dont u know abt tht OOP concepts..?

in other languages we use . (period) sign to access the property and methods of an object, in PHP we use -> for this purpose


so on left side there must b an object of any class and
right side there can b method belonging to that class.....

techietim
10-11-2009, 12:27 PM
As gurmeet said, it is used for object oriented programming in PHP.

As opposed to Java, which uses the . for accessing non-static and static members of an object or class, PHP uses -> for non-static members and :: for static members.

An example of this is:


class myClass
{
public static function myStaticFunction($str)
{
echo $str , '!';
}
public function myPlainFunction($str)
{
echo $str , '?';
}
}
myClass::myStaticFunction('Hello Everyone'); //Static

$myClass = new myClass();
$myClass->myPlainFunction('Hello Everyone'); //Non-Static



PHP.net: Classes and Objects (http://php.net/oop)

jscheuer1
10-11-2009, 02:38 PM
There are no true classes in javascript (what I'm most familiar with that could compare at all) but there are objects and oop and what could loosely be termed classes can be constructed. This (:: and ->) is done in javascript simply by accessing the property/method of the object directly with either the . or [] notation. There are no static versus plain properties, but once again, what could loosely be termed as such can, I believe be constructed - at least private (which cannot be changed) and public (which can) methods/properties can.

The reason I was having such trouble finding out what the -> operator does is that you cannot search on it, at least I couldn't find a way. And it is not listed as an operator in the main php.net operator sections.

Thanks folks. Anyone else feel like adding to this topic though, I'd be interested.

traq
10-11-2009, 07:20 PM
I discovered object-oriented PHP a few weeks ago and it was really difficult to get into, but it's proving to be well worth it. It vastly simplifies the writing and implementation of complicated code. I discovered it via flourish (http://flourishlib.com/), but here are a few tutorials that helped me get my head around it:

good (http://notan00b.com/tutorials/php-object-oriented-programming/)
also good (http://www.massassi.com/php/articles/classes/)
lots of small errors/mistakes, but a good "concept" tutorial if you can troubleshoot it (http://devzone.zend.com/node/view/id/638)

All of these (unfortunately, like any tutorial) use simplified and largely useless code as examples, but hey, that's just how it is.

Useful stuff...
makes code modular/generic/reusable
simplifies control structures (OOP is to functions as functions are to if/else)

As with anything, writing and using it is what is really helping me understand it.

jscheuer1
10-11-2009, 08:48 PM
Thanks traq, I'm actually pretty familiar with oop and its advantages in javascript (and by extension, in any other language that supports oop), I just was wondering about the PHP -> operator and now feel I have a pretty good grasp of it. I could see from the context in which I first encountered it more or less what it was doing, but wanted a formal definition of sorts to concretize that understanding. It seems it is anologous to . or [] when used with javascript objects, the only caveat being that PHP objects have a more formal structure that must be taken into account (visa vis plain and static properties) which I gather PHP shares (to one degree or another) with the vast majority of oop capable languages.

What I'm still wondering is - forgetting about static and plain for the moment, in javascript the . and [] notation as they relate to objects can reference any type of property of the object, be it a string, number, array, function, another object, whatever. I'm wondering if this is also true of the -> operator in PHP, at least as regards plain properties. From what I've seen though, I suspect the answer is yes.

djr33
10-12-2009, 01:40 AM
I've only seen it used for classes. The . of javascript isn't implemented anywhere in PHP as far as I know. [] of course is, but that's just with arrays (or strings acting like them). I've never tried using -> in other contexts, though, so I suppose it might work.

traq
10-12-2009, 02:19 AM
you can reference variables (properties) and methods (functions) of an object (class instance) using the -> symbol. I don't know about other objects, e.g., if you had a method in your class that created (or created an instance of) another class...



$object = new class();
$object->variable = "value";
$object->method($args);

I suppose this makes sense, but I don't know if it would work... one way to find out!


$object = new class();
$object->newclass($otherclass);
$object->otherclass->variable = "othervalue";




<?php
class one{
var $value;
var $var_name;
public function __construct($value){ $this->value = $value; }
public function newclass($var_name, $value){
$var_name = new one($value);
$this->var_name = $var_name;
}
public function getvalue(){ return $this->value; }
public function getsubclassvalue(){ return $this->var_name->value; }
public function getsubclassname(){ return $this->var_name; }
}
$test = new one('value_first_test');
$test2 = $test->newclass('test2', 'value_second_test');

echo $test->getvalue(); //returns "value_first_test"
echo $test->getsubclassvalue(); //returns "value_second_test" (as expected)
echo $test->getsubclassname()->getvalue(); // also returns "value_second_test" (this is the part I wasn't sure about)


?>


so, seems to work. clicky. (http://www.custom-anything.com/test.php)
maybe this isn't exactly what you were wondering about, but it got me thinking.

jscheuer1
10-12-2009, 03:10 AM
I've only seen it used for classes. The . of javascript isn't implemented anywhere in PHP as far as I know. [] of course is, but that's just with arrays (or strings acting like them). I've never tried using -> in other contexts, though, so I suppose it might work.


you can reference variables (properties) and methods (functions) of a class using the -> symbol. I don't know about other objects, e.g., if you had a method in your class that created (or created an instance of) another class...



$object = new class();
$object->variable = "value";
$object->method($args);

I suppose this makes sense, but I don't know if it would work... one way to find out!


$object = new class();
$object->newclass($otherclass);
$object->otherclass->variable = "othervalue";


Will go over this in more detail at some point, but I think you both may have misunderstood me, perhaps I wasn't clear.

I'm abstracting of course. But it seems fairly obvious that regardless of the language, if you have an object and that object has a property, it shouldn't matter what type (string, number, function, etc.) that property is, it should be able to be accessed via the same method/operator as applied to the object that possesses that property. Now, granted - in PHP and most other oop capable languages, there are at least two kinds (public/private or plain/static) of properties, each perhaps requiring a different method/operator for access. I'm just saying that if a given property of an object is of the same kind, that regardless of its type, it should be accessible via the same syntax. Of course, I'm only certain of this in javascript:


var bob = {
your: 'uncle',
whois: function(){return this.your}
};

alert(bob.whois());
alert(bob.your);

Same . operator, one property is a string, the other is a function, both 'work'.

traq
10-12-2009, 03:46 AM
yes. not considering public/private/protected properties.


class bob{
var $your;
function __construct(){
$this->your = "uncle";
}
function whois(){
return $this->your;
}
}

$bob = new bob();
echo $bob->your;
echo $bob->whois();
//both these work, though the second is preferred over accessing the variable directly

//further, if the class looked like this:
class bob{
private $your;
// ...etc
}
//then this:
echo $bob->your;
//wouldn't work. $your would then only be accessible via $bob->whois().

djr33
10-13-2009, 12:48 AM
Hm, interesting point, John, but I guess the basic answer is that PHP is messy. I find it straightforward and very functional, but frequently not at all logical. That's why certain people (ie, Twey, etc.) have strong objections, and they're not wrong. At the same time, of course, to me if it gets done what I need without too much of a headache I'm happy, so PHP is decent as far as I'm concerned.