so I have this class
class A{
public $something['aaa'] = 'soemthing';
}
but then it complains that there is syntax error....
how can I set class variables in PHP as an associative array?
Can't say I'm right saying this.. but you might have to declare it in the constructor:
class A{
public $something; // or $something = array();
function __construct($something){
$this->something['aaa'] = $something;
}
}
That's strange. I don't think that's invalid syntax but it is throwing an error on my end. Maybe the parsre just isn't equipped to handle an property being initialized in that way. When I tried the following equivalent initialization it seemed to work just fine:
<?php
class A {
public $something = array("aaa" => "something");
}
?>
Related
Say that I have a class like this:
class MyClass {
public $variable;
public function __construct($variable) {
$this->variable = $variable;
}
}
and I call it like this (without initializing it, or as far as php is concerned it's not even a class):
$my_class = null;
$v = $my_class->variable;
Is this allowed in php? It would give a big fat null pointer exception in most other languages. If it works, what's the value of variable?
It is "sort of" allowed - you will get a Notice: Trying to get property of non-object and the result will be NULL.
I anyway don't see a point of doing that.
That doesn't make sense...
If you declare a variable $my_class, and makes it NULL, every attribute given to that variable is null.
Of course, for php you're not doing anything wrong. You're just declaring that a variable is null.
This would be different if you declare $my_class as an object of your class MyClass, because you must give an attribute, even if this is null
class MyClass
{
public static $variable;
public function __construct($variable)
{
$this->variable = $variable;
}
}
\MyClass::$variable = null;
echo \MyClass::$variable;
i have a little syntax error which i'm not able to sort out, can anyone help ?
Syntax:
Config Class:
Error:
Do not instantiate private variables like that, you should only be using them for declaring properties and simple values.
You cannot declare a private variable (declaring them a return value from a static functions at least) like that, just do it in the constructor __construct() for the object. You will get the same error for any class you do with a private variable declaration like that and setting it as a return value for any function. Try running the below in PHPFiddle and you'll get the same error.
<?php
class A {
private $hi = B::some_function('hi');
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Instead do something like:
<?php
class A {
private $hi;
public function __construct() {
$this->hi = B::some_function('hi');
}
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Your syntax is incorrect as I've seen in that picture, simply because you didn't have a closing bracket '}' for the class User.
Just try this one.
Use semicolon for every function call as shown below,
$_table = Config::get('tables/users');
$_seassionsTable = Config::get('tables/user_sessions');
It may be fix your issue.
While I was creating a class in php, I experienced this error:
Parse error: syntax error, unexpected '[', expecting ',' or ';' on line 5
A simple example:
<?php
class MyClass
{
public $variable["attribute"] = "I'm a class property!";
}
?>
I already had a look at Reference - What does this error mean in PHP? but this doesn't seem to fit to my case. The problem of all other existing Questions seem to rely to an old PHP Version. But I am using PHP 5.6.3!
What can I do? Am I just sightless?
You can't explicitly create a variable like that (array index). You'd have to do it like this:
class MyClass {
// you can use the short array syntax since you state you're using php version 5.6.3
public $variable = [
'attribute' => 'property'
];
}
Alternatively, you could do (as most people would), this:
class MyClass {
public $variable = array();
function __construct(){
$this->variable['attribute'] = 'property';
}
}
// instantiate class
$class = new MyClass();
I guess you should declare it the way it is shown below :
class MyClass
{
public $variable = array( "attribute" => "I'm a class property!" );
}
Make an array first. Use the code below
<?php
class MyClass
{
public $variable = array("attribute"=>"I'm a class property!");
}
?>
HOpe this helps you
You cannot declare class members like this. Also you cannot use expressions in class member declarations.
There are two ways to achieve what you are looking for :
class MyClass
{
public $variable;
function __construct()
{
$variable["attribute"] = "I'm a class property!";
}
}
or like this
class MyClass
{
public $variable = array("attribute" => "I'm a class property!");
}
I'm fairly new to PHP so I have a small problem as I'm learning:
I built a Class called DataStrip.php
<?php
final class DataStrip
{
public function DataStrip()
{
// constructor
}
public function stripVars($vars)
{
return $vars;
}
}
?>
and then I'm trying to pass the public function stripVars a value:
<?php
include_once ('lib/php/com/DataStrip.php');
echo($projCat);
$a = new DataStrip;
$a->stripVars($projCat);
echo($a);
?>
however, I get back this error:
( ! ) Catchable fatal error: Object of class DataStrip could not be converted to string in myfilepath
... any advice perhaps on what I could be doing wrong here? Right now this is just a test function as I'm trying to get used to OOP PHP. :)
What do you expect it to happen? You're not saving the result of what stripVars returns into a variable:
$result = $a->stripVars($projCat);
print $result;
What you are trying to do is print the object variable itself. If you want to control what happens when you try to print an object, you need to define the __toString method.
if you want to do that... you need declarate the method __toString()
<?php
final class DataStrip
{
private $vars;
public function DataStrip()
{
// constructor
}
public function stripVars($vars)
{
$this->vars = $vars;
}
public function __toString() {
return (string) $this->vars;
}
}
// then you can do
$a = new DataStrip;
$a->stripVars($projCat);
echo($a);
?>
Shouldn't you return to a variable:
$projCat = $a->stripVars($projCat);
When you echo $a you're echoing the object - not any particular function or variable inside it (since when you declare $a you're declaring it as everything in the class - variables, functions, the whole kit and kaboodle.
I also have issues with php oop, so please correct if I am wrong :)
I am trying to make a class from a member variable like this:
<?
class A{
private $to_construct = 'B';
function make_class(){
// code to make class goes here
}
}
class B{
function __construct(){
echo 'class constructed';
}
}
$myA = new A();
$myA->make_class();
?>
I tried using:
$myClass = new $this->to_construct();
and
$myClass = new {$this->to_construct}();
but neither worked. I ended up having to do this:
$constructor = $this->to_construct;
$myClass = new $constructor();
It seems like there should be a way to do this without storing the class name in a local variable. Am I missing something?
Have you tried this?
$myClass = new $this->to_construct;
Are you using PHP 4 or something? On 5.2.9 $myClass = new $this->to_construct(); works perfectly.
In the end it's what you have to live with, with PHP. PHP syntax and semantics are VERY inconsistent. For example, an array access to the result of a call is a syntax error:
function foo() {
return array("foo","bar");
}
echo $foo()[0];
Any other language could do that but PHP can't. Sometimes you simply need to store values into local variables.
Same is true for func_get_args() in older versions of PHP. If you wanted to pass it to a function, you needed to store it in a local var first.
If I read well between the lines you are trying to do something like this. Right?
class createObject{
function __construct($class){
$this->$class=new $class;
}
}
class B{
function __construct(){
echo 'class B constructed<br>';
}
function sayHi(){
echo 'Hi I am class: '.get_class();
}
}
class C{
function __construct(){
echo 'class C constructed<br>';
}
function sayHi(){
echo 'Hi I am class: '.get_class();
}
}
$wantedClass='B';
$finalObject = new createObject($wantedClass);
$finalObject->$wantedClass->sayHi();
--
Dam