/    Sign up×
Community /Pin to ProfileBookmark

ClassName::functionName notation

Hi,

The tutorial I am following used the following code to call a function from another file:

[code=php]echo File::exists(‘test.php’) ? ‘it exists’ : ‘it does not exist’;[/code]

what is that notation, I have never seen it before, why not just call the function, why proceed it by specifying the class name followed by two colons?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 06.2014 — That's how you call a static class method without actually instantiating the class as an object (scope resolution operator).
Copy linkTweet thisAlerts:
@Imam_GainsauthorMay 06.2014 — That's how you call a static class method without actually instantiating the class as an object (scope resolution operator).[/QUOTE]

So I can use that notation in any file to call a method from a class that the file has access to?
Copy linkTweet thisAlerts:
@NogDogMay 06.2014 — Only if the method (or property) is defined as "static".
[code=php]
<?php

class Foo {
public $normal = "normal";
public static $static = "static";
public function regular()
{
echo "regular".PHP_EOL;
}
public static function staticFunc()
{
echo "static method".PHP_EOL;
}
}

echo Foo::$static.PHP_EOL;
Foo::staticFunc();
$foo = new Foo();
echo $foo->normal.PHP_EOL;
$foo->regular();

// not static, should throw a fatal error:
echo Foo::$normal;
[/code]
×

Success!

Help @Imam_Gains spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...