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;
Related
I'm learning OOP in PHP and Laravel. So far my idea is that variable and property are more or less the same. Methods are equivalent to functions. But sometimes I see in many articles that they represent variable and property slightly differently. For example in laravel naming conventions, Model property and variable have different terminologies. What are the differences (if any) between variable and property?
Yes, method is a function.
Model property would be a global variable within the class, so you can use it in all the methods. And depending on the access modifier (private, protected, public) the model property can be used from other classes inheriting/instantiating from that class.
While a variable will be something used within a method and has usage only within the body of that method.
A property is an variable that belongs to the object. The are also called "Members". Imho the main Difference is the Scope. A variable is only available within the method but a property in the whole Class and also in the object if you use public as visibility. http://php.net/manual/en/language.oop5.visibility.php
A property (also called a member variable) in PHP OOP is a variable that exists inside of a class, and must be private, protected, or public. For example:
class Bird {
private $color; // This is a property of the Bird class
}
$var = 123; // This is just a normal variable
So the Model Property that the documentation is referring to is just a member variable of the model.
As far as the difference between functions and methods, they are similar except for 1 major difference: methods are functions that exist inside of a class, and must be private, protected, or public. For example:
class Bird {
public function sing() { // This is a method
echo 'tweet-tweet';
}
}
function sayHello() { // This is a function
echo 'hello';
}
Note: If you do not declare a property or method as private, protected, or public, it is assumed that it is public
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.
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
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');
starting use oop
why:
class user
{
private $pdo;
function __construct()
{
$this->pdo = singleton::get_instance()->PDO_connection();
}
...
}
this works fine. but this:
class user
{
private $pdo = singleton::get_instance()->PDO_connection();
...
}
this does not working. Error parse error, expecting ','' or ';'' in ...
what is wrong with second variant?
See the last sentence of the first paragraph of Properties in the PHP OOP documentation:
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.
In other words, the database handler returned by this statement is not a constant value and therefore will not be available at compile time:
singleton::get_instance()->PDO_connection();
Class properties cannot be assigned at declaration using functions. Scalar values, constants (though not constants of the current class), and arrays only.