Php - Do functions and variables have to be declared public? - php

Do functions and variables have to be declared public or are they public by default?
Class Bread {
$bread = "";
function toast()
{
$bread = "Toasticles!"
}
}
In this example, is both $bread and the function toast() public without actually declaring them as such?
This is a question about instance variables and function visibility

According to the PHP documentation
Properties:
Class properties must be defined as public, private, or protected. If declared using var, the property will be defined as public.
Methods:
Methods declared without any explicit visibility keyword are defined as public.

If you declare $bread without visibility, you will get a parse error:
Parse error: syntax error, unexpected '$bread' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in [...][...] on line x
This is because, as #darkcrystale also mentioned and as PHP documentation states, class properties must be defined as public, private, or protected. If declared using var, the property will be defined as public.
Function toast() will be public by default if you do not specify the visibility explicitly.
But please keep in mind, that doing things explicitly is better, than doing things implicitly. Thus, declaring visibility as public in every case might help those, who read your code after you. If you don't care about those, who might work with your code (btw shame on you in that case), think of another example: you write a lot of code not declaring visibility explicitly and it's public at that time. But if the PHP devs go crazy and change default visibility to private, then most of your code becomes useless (and will not work) for obvious reasons.

Related

Laravel pass config data into trait's property

trait Foo {
private $url = config('api.url');
}
I have a url data set inside of config, however I need to put this value into trait's property. But it's not working. anyone know how to solve this problem?
what I did now is put construct inside of trait
public function __construct(){
$this->url = config('api.url');
}
it's not about traits, it's about php OOP nature itself:
here is the docs:
Class member variables are called "properties". You may also see them
referred to using other terms such as "attributes" or "fields", but
for the purposes of this reference we will use "properties". They are
defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. This declaration may
include an initialization, but this initialization must be a constant
value--that is, it must be able to be evaluated at compile time and
must not depend on run-time information in order to be evaluated.
from the docs example:
// invalid property declarations:
public $var4 = self::myStaticMethod();
public $var5 = $myVar;

Why should variable has public or private or protect but function should not in a class

I what to know why can not I able to define a variable in the class without any thing ? (public, private, protect)
why this has syntax error ?
class myclass {
$var = 'anythig';
}
But this is ok:
class myclass {
function test() { // code here }
}
And finally why it is possible to I define a var without anything in function ?
class myclass {
function test() {
$var = 'anything'; // it has not anythig (public, privare, protect)
}
}
When you use any of the programming languages, you should know the rules, otherwise, the behavior of the program will come strange; for example as in your case, while declaring functions in a class, omitting the visibility keywords implies the function will have the public visibility.
About the property visibility:
Class properties must be defined as public, private, or protected. If
declared using var, the property will be defined as public.
And about the method visibility:
Class methods may be defined as public, private, or protected.
Methods declared without any explicit visibility keyword are defined
as public.
And finally about the defining variables, I believe you need to read about the variable scope.
As per request, about the comment above regarding static vs private:
staticness of a class member is intended to deal with the lifetime of the matter (the matter exists regardless of existence of any instance of a class), while privateness is about the visibility of the matter (for example: the existent matter cannot be accessed when it has the private visibility.)
The two are different concepts and are not mutually exclusive (you may use them together.) Mixing these concepts, makes me believe that you're in an urgent need of reading some OOP materials.

PHP: Cannot set a value for a private static property

I have a very simple issue where I am trying to set a private static property value made up of a constant appended with some text like this:
private static $cssDirectory = APP_ROOT.'css/';
I am getting a syntax error. I can fix this by making the private variable not static and assign a value with a constructor for example but since I want it static I am curious what can I do about it. I can also make a constant for the whole value and use that but again I am curious why I can't do it like I tried. Maybe I am doing something wrong also. Thanks.
From the PHP docs
Class member variables are called "properties"... They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value --that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
Concatenation is a run-time operation.
You don't need to instantiate and set the property value in the constructor.... you can write a static setter method instead
Note also that PHP 5.6 does allow this type of initialization of class properties
EDIT
Example of a static setter method:
private static $cssDirectory;
public static setCssDirectory() {
self::$cssDirectory = APP_ROOT.'css/';
}
And then you just call
myClassName::setCssDirectory();
before anything else

While making the static class for database getting this error

Here is my class
class Databases {
public $liveresellerdb = new Database('1host1','user','pswd','db');
}
the error i am getting is
Parse error: syntax error, unexpected T_NEW in /home/abhijitnair/sandbox/newreseller/Databases.php on line 33
why this error is coming?
Properties may not be preset with runtime information.
Quoting PHP Manual:
Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
<?php
class Databases {
public static $liveresellerdb;
}
Databases::$liveresellerdb = new Database('1host1','user','pswd','db');
?>
This is how you initialise a static member...
Because you forgot to write the static keyword to actually make the property static.
In addition, you can't initialise static properties with expressions like this. Here's a workaround.
you cannot assign object during the class preperation stages, only the class instantation:
class Databases
{
public $liveresellerdb;
public function __construct()
{
$this->liveresellerdb = new Database('1host1','user','pswd','db');
}
}
anything within the constructor can be generic PHP code, outside the function and instead the class body has specific laws.
if you require the database's to be static then you have to set / access them differently.
class Databases
{
public static $liveresellerdb;
}
Databases::liversellerdb = new Database('1host1','user','pswd','db');

What's the default inheritant type of a member function in PHP?

class foo implements Countable {
function count() {
# do stuff here
}
}
What's the type of count,public,protect or private?
Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.
Same behaviors also apply for class properties.
Taken from PHP: Visibility
As Bart has noted in his comment, although PHP will assign the visibility for you (if one is not explicitly assigned), it is strongly recommended for good practice and coding standards to assign the visibility for yourself.
It's type is public. In php if you don't specify scope for methods, it is assumed public.
Public....unless specified otherwise.

Categories