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.
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.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
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.....
jscheuer1 (10-11-2009)
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:
PHP Code: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
jscheuer1 (10-11-2009)
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.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
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, but here are a few tutorials that helped me get my head around it:
good
also good
lots of small errors/mistakes, but a good "concept" tutorial if you can troubleshoot it
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.
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.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
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.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
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...
I suppose this makes sense, but I don't know if it would work... one way to find out!PHP Code:$object = new class();
$object->variable = "value";
$object->method($args);
PHP Code:$object = new class();
$object->newclass($otherclass);
$object->otherclass->variable = "othervalue";
Edit:
so, seems to work. clicky.PHP Code:<?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)
?>
maybe this isn't exactly what you were wondering about, but it got me thinking.
Last edited by traq; 10-12-2009 at 04:08 AM.
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:
Same . operator, one property is a string, the other is a function, both 'work'.Code:var bob = { your: 'uncle', whois: function(){return this.your} }; alert(bob.whois()); alert(bob.your);
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
yes. not considering public/private/protected properties.
PHP Code: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().
Bookmarks