While making the static class for database getting this error - php

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');

Related

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

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.

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.

Access one object attribute in another attribute

When I try to do the following I get a syntax error, unexpected T_VARIABLE. What am I doing wrong?
class myObj {
public $birth_month;
public $birthday = array('input_val' => $this->birth_month);
}
I also tried
class myObj {
public $birth_month;
public $birthday = array('input_val' => $birth_month);
}
You cannot use an expression to initialize a class property. It must be a constant value, or you must initialize it in the constructor. That's the source of your syntax error.
class myObj {
public $birth_month;
public $birthday;
// Initialize it in the constructor
public function __construct($birth_month) {
$this->birth_month = $birth_month;
$this->birthday = array('input_val' => $this->birth_month);
}
}
From the docs on class 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 your first attempt, using $this outside an instance method would not have been supported even baring the compile-time limitation of property initialization, since $this is only meaningful inside instance methods.
$this does not exist outside a non-static method of your class. Also, at initialization time, there is no $this yet. Initialize your array in the constuctor method.

why can not use property instead of __construct?

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.

Categories