What does -> mean in PDO PHP? [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I'm a complete beginner in learning PDO for PHP and I actually haven't learned MySQL or MySQLi yet.
(Please take a look at the code below) I'm trying to make sense of what this " -> " arrow means and I couldn't find an answer anywhere else. Is the arrow semantically equivalent to the action word "perform" in every day english?
E.G for the codes below, $stmt (perform) -> closeCursor();
Code:
$stmt = $db->prepare($sql);
$stmt->execute(array($title,$entry));
$stmt->closeCursor();
Thank you.

The arrow is part of PHP's object syntax, it's saying:
$object->method();
In English is:
Run method on object
It's also used for accessing properties.
As PDO is a class in PHP and variables like $db are instances of that class, you're able to make use of the methods and properties in those instances.
Check out PHP's object docs for more info on the subject, and if you're new to Object Oriented programming then you'll need to research the subject.

That is php's object notation equivilent to . in Java and Javascript. Basically it's used to access a method or property of an object.
See the Objects and Classes documentation.

Related

What is the meaning of "$cm->course" in moodle? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I am new at PHP and currently now i am working on moodle LMS tool.
I am referring view.php of scorm package and dont know why $cm->course this is used in php.
Please help.
Thanks in Advance
Have a look here
"->" means accessing the $cm object property course
$cm is an object, and course is a variable. Here you can see brief of PHP syntax with objects:
http://php.net/manual/en/language.types.object.php
PHP has two Object Operator namely -> and ::
-> is used when you are trying to call a method on an Instance and / or access an Instance property.
:: is used when you want to call a static method or call a parent
class's version of a method within a child class.
Reference: https://www.codeproject.com/questions/196270/what-is-meant-by-symbol-in-php
Note: I googled your exact question and got this result!
It is "object operator" - T_OBJECT_OPERATOR. It is used to access properties and methods of class on an object.

What is the "->" in code? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What is this called in php: -> [duplicate]
(4 answers)
Closed 9 years ago.
I am trying to learn how a specific project works and while I can find most functions online, I found one that stumps me.
I see "->" appear every so often in the code, but have no idea what it does.
What does "->" mean in PHP or Joomla?
It's the object operator in PHP. It is used to access child properties and methods of classes. Its Javascript and Java equivalent is the . operator. It would be used in PHP like this
class foo{
public $bar="qux";
public function display(){
echo $this->bar;
}
}
$myFoo=new foo();
$myFoo->display(); //displays "qux"
Its like the . operator in C++ and Java. Refers to members within a class. C++ also uses the -> to access members when the variable preceding the -> is a pointer to a class rather than an instance of the class.
-> is the way used to call a method.
In C, C++, C#, Java you use . (dot) notation to call a method:
operation.sum(a, b);
In php you do it using ->
operation->sum(a,b)

PHP: why are some internal functions missing from `get_defined_functions`? [duplicate]

This question already has answers here:
What is the difference between a language construct and a "built-in" function in PHP?
(4 answers)
Closed 8 years ago.
PHP has a large number of batteries-included functions, e.g. functions on arrays. Some of these, like each, are present in get_defined_functions()['internal']. Others, like reset and many others, are not present at all. However, they are treated as functions in every other way: they are not documented as "language constructs" or keywords; I can call them using the "variable function" feature; function_exists("reset") returns true; if I try to redefine them (e.g. function reset() { ... }), I get an error about redeclaration, rather than a syntax error; and so on.
Why are these functions not listed by get_defined_functions? Are they not actually functions? If not, what are they? If they are functions, then what actually is it that get_defined_functions is listing? In either case, how do I list the things that don't appear in get_defined_functions?
Quite a short answer: Reset is present in get_defined_functions()['internal'].
Look at [1532] in this fiddle: http://phpfiddle.org/main/code/h5n-ndx

Can I use the arrow operator on a new instance of an object? [duplicate]

This question already has answers here:
In PHP, can you instantiate an object and call a method on the same line?
(9 answers)
Closed 9 years ago.
The following PHP code is valid:
$a = new MyClass();
$a->myFunction();
Is there a valid way to combine these two statements? My first attempt understandably results in a syntax error:
new MyClass()->myFunction();
I then added brackets around the created object, and that ran fine on PHP 5.4.17 but not on PHP 5.3.26:
(new MyClass())->myFunction();
At what point is this syntax accepted, or should I abandon this idea and just use two lines. My actual use case is more complicated and it would make the code much neater if I didn't have to keep creating single-use variables all the time.
I am unable to find anything about this in the documentation.
This answer comes directly from this question also asked here on SO.
The feature you have asked for is available from PHP 5.4. Here is the list of new features in PHP 5.4:
http://docs.php.net/manual/en/migration54.new-features.php
And the relevant part from the new features list:
Class member access on instantiation has been added, e.g. (new Foo)->bar().
(new MyClass())->myFunction();
is justfine since PHP 5.4. You can use it, but i think two-line approach is cleaner, backwards compatible with older PHP versions and won't confuse newcomming programmers who read your code :)
Added in PHP 5.4.0. Class member access on instantiation has been added, e.g. (new Foo)->bar().
http://docs.php.net/manual/en/migration54.new-features.php
The (new MyClass())->myFunction() notation is introduced in PHP 5.4 (including some other short-hand notations, see the new features-page).
One workaround (which I don't recommend to misuse this in all situations) is to have a static method in MyClass:
class MyClass {
public static newInstance() {
return new self();
}
public function myFunction() {}
}
MyClass::newInstance()->myFunction();

Difference between :: and -> in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the difference between :: (double colon) and -> (arrow) in PHP?
Reference - What does this symbol mean in PHP?
I'm quite decent with PHP, but only procedural. So I decided to venture forth into learning object oriented. I'm getting the hang of it, and liking it quite well.
On PHP.net, I've always seen object oriented as mysqli::query.
However, every example I've seen uses mysqli->query as well as what I have always used.
Today, I ran across actually seeing :: used in a class example.
So now, my question is, is there a difference between :: and ->?
Is it like the difference between " and '?
:: is for calling static methods, -> is for instance methods
:: is the "scope resolution operator" (also aptly named Paamayim Nekudotayim), and is usually used to do a static (which means that you'll call the method in the context of the class itself, not the object) method call. There are however exceptions to this rule, such as attempting to call a parent method from an overriden method:
parent::foo(); // uses same context as when the method itself was called
It'll also allow you to reference static properties of the class, such as static properties and constants.
ClassName::FOO;
ClassName::$property = "bar";
-> is however used to reference a property or method in the actual object instance, and will always require an object instance to the left of the operator (such as $this).

Categories