This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I've come accross this notation in PHP magento 'Mage::run()'.
What does :: mean? Can't seem to find a simple explanation anywhere.
It allows you to call a static function/method in a class, without making an instance of the class:
class myClass{
static function myFunction(){
echo"foo";
}
}
now call it like this with ::
myClass::myFunction;
with a public function, you would have to make an instance:
$mycalssinstance= new myCalss;
//then call it
$mycalssfunction=$mycalssinstance->myFunction();
It's Scope Resolution Operator (also called Paamayim Nekudotayim). It allows you to access constants or static methods.
Call to a static (class) method. You can call this without an instance of the class.
http://us.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
It is a reference to a static php function
http://php.net/manual/en/language.oop5.static.php
Related
This question already has answers here:
Difference between double colon and arrow operators in PHP? [duplicate]
(3 answers)
What's the difference between :: (double colon) and -> (arrow) in PHP?
(6 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 3 years ago.
I´m trying to create a logging class which will be accessible in all Classes around the PHP app by
logger::log(something);
and this will add next row into my logfile (the part with inserting into file is easy for me). I saw the double colon in DIBI (database framework). It is cool, because I can use dibi::dataSource("") whereever I need. But don´t know how to do this in my application.
Right now I have something in some class (I have more similar classes in the app) like (shorted):
Class DoSomething {
function runTests() {
logger::log("Test started");
// do the magic
logger::log("It ends");
}
}
In index.php I have something like:
// init
$app = new DoSomething;
$app->runTests();
...
And I would like to have in index.php some code, which will add the accessibility of logging function in class with "logger::log();". But don´t know how to do this. Can you please help me?
Maybe it can somehow work with "extends", but is there any easier solution?
I have tried to read - https://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php but still not sure, how to do this.
Thank you.
The double colon allows access to static function and constants in a class.
Change your class method to:
static function runTests() {
...
and then call it like this
DoSomethin::runTests();
If I understand correctly your question, what you are looking for is a static method.
This kind of method would allow you to call your function without instantiating an object beforehand (using new Logger)
To do that, your function should be declared as public static. Here's an example:
public static function test()
{
// Method implementation
}
More documentation here : php static functions
This question already has answers here:
PHP: What if I call a static method in non-static way
(3 answers)
Closed 6 years ago.
I am trying to wrap my head around the static keyword in PHP. Here I wrote a small piece of code where my person1 instance of Human can call a static method called sayRealname(). But when I try to call a public static variable it gives an error. What is the reason for that?
class Human{
public static $age=34;
public static $name='humpty dumpty';
protected static $realname='al';
public static function sayRealname(){
echo self::$age;
}
}
$person1=new Human();
echo $person1->name; // error
$person1->sayRealname(); // prints 34
When you're accessing static content, you need to use the static operator ::.
On the other hand, the arrow operator -> is meant to be used in an instance context, which belongs to the particular instance upon it's being called.
You can read more about this in this part of the documentation.
This question already has answers here:
Default visibility of class methods in PHP
(6 answers)
Closed 8 years ago.
I've been learning OOP in PHP and I'm wondering what happens when you instantiate a class with functions that are not declared as "public", "private", or "protected"? Here's an example:
//file1
class foo {
function doSomething() {
return "stuff";
}
}
//file2
include(file1.php);
$bar = new foo;
P.S. I'm not sure if it's relevant, but I'm learning OOP PHP because a website I'm helping develop has been using the dolphin framework heavily and has been using this type of
You're asking what happens? Nothing. You can still use them outside of the class (not private). And you can still overwrite them (not protected).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
where we use object operator “->” in php
In PHP 5, what are the advantages of typing $class::method() instead of $class->method()?
As in any performance or functional differences. Or is this just a way of forcing code non-PHP4 friendly because of the complete rewrite?
In PHP5, the two aren't interchangeable.
Static method calls will perform faster than non-static calls (over many iterations) but then the method is called in the static context and there is no object available to the called method.
The only reason PHP lets you call a non-static method using the static notation was for backwards compatibility in PHP 4 (because PHP 4 didn't have the static modifier for functions, or public/protected/private). If you do call a non-static method statically, you get a warning about "Strict Standards" output, and eventually this may fail with a fatal error.
So the answer really is to call the method the way it was supposed to be called. If it is a static method in PHP 5, then call it statically Class::method(), if it is a public method, then call it using the object $class->method().
Consider this code (run in PHP 5):
class Foo {
protected $bar = 'bar';
function f() {
echo $this->bar;
}
}
echo Foo::f(); // Fatal error: Using $this when not in object context
$class::method() calls a static method of the class whereas $class->method() calls a public standard method of the class.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: How to chain method on a newly created object?
I can create an instance and call it's method via:
$newObj = new ClassName();
$newObj -> someMethod();
But is there any way I can do it in a shorter notation, an anonymous instance? I tried this:
(new ClassName())->someMethod();
But it doesn't seem to work as expected.
Additional Info: The method I want to call is public but not static.
PHP 5.4 supports it.
If you can't update you can workaround like this:
function yourclass($param) {
return new yourclass($param);
}
yourclass()->method();
Don't forget that your constructor must return $this;
Not that i know of.
But! - You could implement a Singleton Pattern and then call:
ClassName::getInstance()->someMethod();
Or, to cut it short ;)
ClassName::gI()->someMethod();
If someMethod does not refer to $this, you could also simply call it as a static function, though it wasn't defined as one:
ClassName::someMethod();
If the method is static and doesn't rely on any class variables (maybe you've put it in a class just for organisation purposes), you can simply call it staticly as phil is demonstrating with getInstance:
ClassName::someMethod()