PHP use variable of class in other class - php

I basically know php but i am new to all that classes-stuff. For now - love it.
Here is my problem:
I'm writing a class to do all that stuff around the account-managements. (e.g. create new account, get account details, check if account exists .... )
Within that class i need to do some MySQL-requests. Therefor i i'm using the medoo-class (http://www.medoo.in).
class acc{
// Attributes
public static $account;
public $pw;
protected $error;
public function acc_exist() {
$database = new medoo();
$acc_count = $database->count("table_accounts", ["column_account" => acc::$account]);
if ($acc_count == 0) {return true;} else {$this->error .= "Account exists already!";};
}};
Please note the line:
$database = new medoo();
and
$acc_count = $database->count("table_accounts", ["column_account" => acc::$account]);
here i bring in medoo. And ["column_account" => acc::$account] acctually works. As i read in some other posts, i made $accounts public static.
now i call my class like this:
$my_acc = new acc();
$my_acc->account = 'Luci';
$my_acc->acc_exist();
i need to work like that. Doing some acc($account) is difficult in context of the rest of my code.
But as i expected, i get an error:
Strict Standards: Accessing static property acc::$account as non static
clear to my that static holds the var's value. so i will need some other way. Anyone got an idea?
best, Lox

I don't think you need to have $account as static, that wouldn't make sense with the way you're probably going to be using this code, try having public $account; and then use ["column_account" => $this->account]
So:
class acc{
// Attributes
public $account;
public $pw;
protected $error;
public function acc_exist() {
$database = new medoo();
$acc_count = $database->count("table_accounts", ["column_account" => $this->account]);
if ($acc_count == 0) {return true;} else {$this->error .= "Account exists already!";};
}};
Here's more information on how to use static properly: Static Keyword in PHP

You are calling a variable that doesn't exist.
You declared $accout as public and static.
But you attempt calling $account.
Replace:
$my_acc->account = 'Luci';
With:
$my_acc->accout = 'Luci';

Vex is right. Take off the static keyword and use ["column_account" => $this->account] instead.
Bests,
B.

Related

how to instantiate the variable you passed in the constructor in Laravel Jobs

I created a protected $companyID; in my Job class then in the __construct function I passed an $id but when I try to instantiate the variable in my handler function its null
protected $companyID = NULL;
public function __construct($ID) {
$this->companyID = $ID;
}
Then when instantiating the class?
$ProcessOutgoingSMS = new ProcessOutgoingSMS();
$ProcessOutgoingSMS->dispatch(3);
You don't pass any argument to the constructor, maybe this is the source of your problem.
$ProcessOutgoingSMS = new ProcessOutgoingSMS(2);
As more a general code style comment, I would also recommend not having variables with uppercase first characters. $id and $processOutgoingSMS.
You should pass 3 into constructor:
$ProcessOutgoingSMS = new ProcessOutgoingSMS(3);
$ProcessOutgoingSMS->dispatch();
and not
$ProcessOutgoingSMS = new ProcessOutgoingSMS();
$ProcessOutgoingSMS->dispatch(3);
Where I instantiate the class and try to perform the injection console/kernel.php
$idobject = (object)['id' => 3];
new ProcessOutgoingSMS($idobject);
then in the class which am trying to perform the functionality made the var a static var
static $companyID = 0;
now the constructor looks like
public function __construct($company) {
self::$companyID = $company->id;
}
then in the handle() function I can access whatever value was injected
dd(self::$companyID);
thanks guys

Set A variable from other Variable in a class without function

I started using classes in PHP, and I have the following code:
class webConfig
{
public $SALT1 = "3525";
public $SALT2 = "2341";
public $SALT3 = $SALT1.$SALT2;
}
But this gives me 500 Internal Server Error, which means that I am doing something wrong.
Is there any way of doing this without using any function, including construct?
This does not work aswell:
class webConfig
{
public $SALT1 = "3525";
public $SALT2 = "2341";
public $SALT3 = $this->SALT1.$this->SALT2;
}
In PHP; Expressions are not allowed as Default Values of any Property. For this reason, PHP is simply saying that you should not even imagine doing that in the first place. But, fortunately then; this is why you have the Constructor Method — to help you initialize some class Properties, assign Default values to Member Properties using Expression (like in your case), etc.... Using a Constructor, your class could look like the Snippet below and it is expected to work as well:
<?php
class webConfig {
public $SALT1 = "3525";
public $SALT2 = "2341";
public $SALT3;
public function __construct(){
// SET THE VALUE $SALT3 HERE INSTEAD
$this->SALT3 = $this->SALT1 . $this->SALT2;
}
}
Alternatively; you may even initialize all the Properties of the Class within the Constructor like so:
<?php
class webConfig {
public $SALT1; //<== NOTICE THAT THIS PROPERTY IS NOT INITIALIZED.
public $SALT2; //<== NOTICE THAT THIS PROPERTY IS NOT INITIALIZED.
public $SALT3; //<== NOTICE THAT THIS PROPERTY IS NOT INITIALIZED.
public function __construct(){
$this->SALT1 = "3525";
$this->SALT2 = "2341";
$this->SALT3 = $this->SALT1 . $this->SALT2; //<== YET AGAIN; SET THE VALUE
//<== OF $SALT3 LIKE BEFORE
}
}

Using $this when not in object context into the same class

I'm trying to build a View class for Smarty templates so I could call templates like in Laravel View::make('template');
But, for first time I'm getting this error. I found plenty of responses fixing the issue but I cannot fix mine. I don't know why so I'm starting to be little bit fed up... hehehehe
This is the class:
class View {
public $engine;
protected $tmpl_folder = 'tmpl';
protected $tmpl_compiled = 'tmpl_c';
protected $cache = true;
protected $force = true;
function __construct() {
$this->engine = new Smarty();
$this->engine->compile_check = $this->cache;
$this->engine->force_compile = $this->force;
$this->engine->template_dir = './' . $this->tmpl_folder . '/';
$this->engine->compile_dir = './' . $this->tmpl_compiled;
}
static function make($t, $args = '') {
if(!empty($args))
$this->engine->assing($args);
$this->engine->display($t.'.tpl');
exit();
}}
Error is launching in method make(), line <b>$this->engine->assing($args)</b>.
I tried to change declaration of $engine variable to public, private, protected and static... nothing...
I declared make() like public, public static, like in the example above and the same with no "static".... nothing...
I tried to change $this-> for self::... nothing...
I don't know what else can I do! Some advice please!
to get this working quickly instantiate your View class as an object like this:
$obj_view = new View();
then make your call to the make() method like this:
$obj_view->make('template');
I'd also declare scope for your methods (functions) (both of them should be public) - you did it for the properties (variables), so why not for the methods as well and remove static from the make()
why are you calling exit() in make()? I hope this is for debug ('Dead programs tell no lies')
assing() made me lol, a great typo! :)
TIP: avoid using bad language in code (in variable names, comments or debug) these are viewable by other developers and can, unintentionally, leak to the end user - it's not professional!

How can I access a private variable in a class without changing its visibility

I am wondering is it possible for me to access the _staff_id variable without me having to change the declaration to public (I have access to change this but its not my code and im assuming it was made private for a reason, however i am still tasked with getting this information)
MyObject Object
(
[_staff_id:private] => 43
)
Using a public get function. E.g.:
class MyObject {
private _staff_id = 43
public function get($field) {
return $this->$field;
}
}
$myObject = new MyObject;
$staff_id = $myObject->get('_staff_id');
This allows you to access the variable without the ability to overwrite its value.

basic php: why cannot access empty property here?

I am have an instance variable w/ a global scope set in a PHP class. The variable gets set successfully in the constructor. But when I got to access in a getter method I get a PHP fatal error. I'm fairly new to php. Why is $sid not getting set?
class Wrapper
{
private $sid = null;
/**
public function __construct($data){
// Retrieve client account automatically
$sid = (isset($data['client_id'])) ? $data['client_id'] : null;
echo("set sid to");
echo($sid);
$this->client = new Account($sid); //this is getting set properly
}
...
public function getSID()
{
return $this->$sid;
}
The code that uses the class looks like this (it is a unit test with PHPunit):
public function testGetSubsctions(){
$clientObject = ClientModel::getInstance(7)->read();
$data = array('client_id' => $clientObject->id);
$this->assertEquals($data['client_id'], 7);
$hello = new Wrapper($data);
$this->assertEquals(7, $hello->getSid());
}
The code above throws the following error:
Cannot access empty property in /path to/wrapper.php on line 243
The code on line 242 is getSid, below
public function getSID()
{
return $this->$sid;
}
in the first part of the code you show, we can see return $this->$sid; but $sid is not defined. non-static property has to be called without the $
The correct syntax is :
public function getSID()
{
return $this->sid;
}
EDIT: see the php documentation for more help on the usage of $this->, static and other things related to OOP in php, and more specifically :
About properties
about static keyword (just for your information to see the difference)
Here is how you set $sid;
public function __construct($data){
// Retrieve client account automatically
$this->sid = (isset($data['client_id'])) ? $data['client_id'] : null;
echo("set sid to");
echo($this->sid);
$this->client = new Account($this->sid); //this is getting set properly
}
The variable is part of the instance so you have to use the $this keyword. Simply using $sid refers to a new local variable.
And also there is no $ sign before sid:
public function getSID()
{
return $this->sid;
}

Categories