This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does this php construct mean: $html->redirect(“URL”)?
Hi, I've been looking for what this operator "->" does, but I can't seem to find a reference to it, only people using it in come and I can't make out what its doing, an explanation would be appreciated.
The -> is used with object methods/properties, example:
class foo{
function bar(){
echo 'Hello World';
}
}
$obj = new foo;
$obj->bar(); // Hello World
More Info:
http://de.php.net/manual/en/language.oop5.basic.php
http://php.net/manual/en/tokens.php
It's for classes.
See here:
http://de.php.net/manual/en/language.oop5.basic.php
-> operator access properties and methods of an object.
Probably you should read PHP OOP introduction: http://php.net/manual/en/language.oop5.php
Expanding on sarfraz's answer to demonstrate how to access properties:
class foo{
public $value = "test";
function bar(){
//// code
}
}
$obj = new foo;
$obj->bar();
echo $obj->value; //displays "test"
Related
Why can't I use a placeholder to call a static property from a class?
To illustrate:
class Foo {
public $obj_property = 'works';
public static $static_property = 'does not work';
}
/**this works*/
$foo = new Foo();
$obj_prop = 'obj_property';
echo $foo->$obj_prop;
/**this fails*/
$static_prop = '$static_property';
echo Foo::$static_prop;
The error returned is Access to undeclared static property: Foo::$static_prop.
I understand that it is trying to look for $static_prop which doesn't exist. But why is it doing so?
Should it not be looking for $static_property - bc that is the value of $static_prop??
I'm sorry if this has been asked already - looked all over but couldn't find it.
(Edit: Related question found here which asks how to access the vars. My question is more asking the why than the how. But the other question is definitely related and helpful.)
The expression Foo::$static_prop means "the static variable static_prop defined in the class Foo
This expression works :
$static_prop = 'static_property';
echo Foo::$$static_prop;
with the double dollar expression, first $static_prop is evaluated to 'static_property', then Foo::$static_property can be found.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
many times I've seen this symbol ->
What is it? seems a logical operator or something
Complete example:
get_categories->category_count
It's called the object operator.
For example:
$myObject = new stdClass();
$myObject->Hello = "world";
echo $myObject->Hello; //Returns world
You can also use this operator to call properties or methods from classes. For example:
Class MyClass {
public function hello(){
return "world";
}
}
echo (new MyClass())->hello(); //Returns world
It is called as PHP object operator (T_OBJECT_OPERATOR).
Example:
$car = new Car();
$car->color = 'Red';
echo $car->color; // returns the color of the car.
This question already has answers here:
Calling a function within a Class method?
(10 answers)
Closed 9 years ago.
please help me with this example.. I want to call the function inside the class..
FUNCTION
class myTest(){
function Me(){
$x = 2;
}
}
is this the answer?
$myval = new myTest();
echo $myval;
thank you
You need to first instantiate a new object of type 'myTest' like so:
$myObj = new myTest;
And then you can use the function like so:
$myObj->Me();
Note that at present, the function returns nothing so you'll get a 'blank screen'.
If you changed your function to read:
return "Hello";
Then you would get 'Hello' printed on screen by using the above.
I hope that helps?
$myval = new myTest(); here $myval is object of the your Class .
You can call the inside function with just $myval->Me();
Working a lot with JS I have come to love closures, so I was pleased to learn that there are closures in PHP also. However I just can't get this stuff to work, what's wrong with this piece of code?
class Foo {
public $Bar;
public function Foo() {
$this->Bar = function() { echo "Hello World"; };
}
};
$F = new Foo();
$F->Bar();
I keep getting PHP Fatal error: Call to undefined method Foo::Bar() errors.
This has been discussed a lot on SO already (see e.g. this answer). This should do the trick:
$b = $f->Bar;
$b();
Yes, it is that stupid. You could use call_user_func() to put in in one line (see jlb's answer to this question), but the ugliness remains.
If you want a one-line solution to replace
$F->Bar()
try this:
call_user_func($F->Bar);
PHP has separation between methods and fields. In fact, you can have a method and a field of the same name at the same time:
class Foo {
public $Bar;
function Bar() { echo "hello\n"; }
};
$F = new Foo();
$F->Bar = 42;
$F->Bar(); // echoes "hello"
So you can see that, to avoid ambiguity, there must be a separate syntax between calling a method with that name, and accessing a field with that name and then calling that as a function.
If PHP had better syntax, they would support ($F->Bar)(), i.e. function call operator on any expression, but currently only variables can be "called".
PHP isn't liking the $F->Bar notation for accessing the closure.
If you change this slightly to
$t = $F->Bar();
$t();
then it works.
i'm new to php and am wondering - is it possible to initialize a class with parameters?
like $obj = new myClass('myID');
i've tried but it gave me an error.
maybe someone can point me to some good tutorials.
thanks
Yes it is, you need to pass the variables through the constructor:
class SomeClass
{
function __construct($some_var)
{
}
}
Please note that in older versions of php the constructor needs to have the name of the class, it´s __construct() since php 5.
Yes, you need to use a class constructor.
Example:
<?php
class ClassWithArgs
{
function __construct($argument1, $argument2)
{
echo "arg 1 = $argument1\n";
echo "arg 2 = $argument2\n";
}
}
$object = new ClassWithArgs('one', 'two');
?>
Example Output
arg 1 = one
arg 2 = two
I suggest you also take a look at the PHP5 OOP documentation, it includes simple examples to get you going: http://php.net/manual/en/language.oop5.php