PHP How to access variable from other variable inside class? - php

Here is my class:
<?php
class myClass {
private $a = 1;
private $b = array(
'a' => $this->a
);
public function getB() {
return $this->b;
}
}
$myclass = new myClass();
var_dump($myclass->getB());
I want to access variable $a in variable $b. But this shows this error:
( ! ) Parse error: syntax error, unexpected '$this' (T_VARIABLE) in
C:\xampp\htdocs\test1.php on line 5

You are not allowed to assign a variable property this way. The best way is to probably assign the variable to the array in the constructor instead. So, like this:
<?php
class myClass {
private $a = 1;
private $b = array();
public function __construct() {
$this->b['a'] = $this->a;
}
public function getB() {
return $this->b;
}
}
$myclass = new myClass();
var_dump($myclass->getB());

You can access variables by constructor.
Here is some code:
class myClass {
private $a;
private $b;
public function __construct(){
$this->a = 1;
$this->b = array('a'=>$this->a);
}
public function getB() {
return $this->b;
}
}
$myclass = new myClass();
var_dump($myclass->getB());

Related

how to change non static variable to static method in php

I am calling constant variable like this, but it show errors, How to solve this?
I don't calling it like these code below,
$b = new A()
$b::$test
here my code
class A {
const test = 4;
}
class B {
private $a = null;
public function __construct(){
$this->$a = new A();
}
public function show(){
echo $this->$a::test;
}
}
$b = new B();
$b->show();
How to calling static variable test in class A?
Thanks in advance
Every thing is fine except $this->$a::test; and $this->$a = new A();. You have to use property without $ sign like below
class A {
const test = 4;
}
class B {
private $a = null;
public function __construct()
{
$this->a = new A();
}
public function show()
{
echo $this->a::test;
}
}
$b = new B();
$b->show();

PHP Class with properties that has objects be accessible to these objects

I have a main php class such as this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass();
$this->b = 'some string';
}
}
is there a way the class which is stored in the $a (SomeClass) property can access the $b value which is actually a property which is stored in the class that initiated $a (MyClass) ?
You could do something like this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->a = new \SomeClass($this);
$this->b = 'some string';
}
}
class SomeClass {
public $mc;
function __construct(MyClass $mc)
{
$this->mc = $mc;
}
}
$myClass = new MyClass();
echo $myClass->a->mc->b;
The output would be: some string
You can do something like this:
class MyClass {
public $a;
public $b;
function __construct()
{
$this->b = 'some string';
$this->a = new \SomeClass($this->b);
}
}
class SomeClass{
function __construct($b)
{
echo $b; // it will become a $this->b as given while creating new class in MyClass
}
}

Cannot create object in class PHP

I try to create object in PHP class, but i get some interesting errors in IDE, like unexpected ( token etc. Here is my code:
class A {
public $a = 1;
}
class B {
$aa = new A();
}
Where is the problem?
In PHP, you can only assign "fixed" values to properties in the class definition.
class A {
public $a = 3; // will work
public $b = "hello"; // will work
public $c = foo(); // won't work
public $d = new Foo(); // won't work
}
If you want to do so, you can use the __construct() method which will be called every time a new instance is created or any other method that you call.
class B {
public $aa; // define visibility of $aa
function __construct() {
$this->aa = new A();
}
}
You need to make a constructor on class A
class A {
function __construct() {
$this->a = 1;
}
public function returnA() {
return $this->a;
}
}
$aa = new A();
echo $aa->returnA();
Try to create a constructor in class A and see if it works:
class A {
public $a;
function __construct()
{
$this->$a = 1;
}
}
class B {
$aa = new A();
}

PHP Fatal error: Using $this when not in object context in C:/xampp/htdocs/oops/1.php on line 9?

I need to assign b value in a inside the method onec, but its failing. Please let me know what I am doing wrong here:
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$this->a = $this->b;
return $this->a;
}
}
echo One::onec();
?>
Use the self keyword. The $this keyword is not accessible under static context. Also, you should make your variables static
Like this..
<?php
class One {
public static $a = 10;
public static $b = 20;
public static function onec() {
self::$a = self::$b;
return self::$a;
}
}
echo One::onec();
You use $this in static function.
http://www.php.net/manual/en/language.oop5.static.php
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$obj = new One();
$obj->a = $obj->b;
return $obj->a;
}
}
echo One::onec();
Use this code
class One {
public $a = 10;
public $b = 20;
public function onec() {
$this->a = $this->b;
return $this->a;
}
}
$obj = new One();
echo $obj->onec();

shared variable across multiple class instances that I can change outside the class

the code explains it better:
class Class{
$var = 0;
function getvar()
echo $this->var;
}
}
$inst1 = new Class();
// I want to change $var here to 5
$inst2 = new Class();
echo $inst2->getvar(); // should be 5
Is it possible
Static. http://php.net/manual/en/language.oop5.static.php
class MyClass {
public static $var = 0;
function setVar($value) {
self::$var = $value;
}
function getVar() {
return self::$var;
}
}
echo MyClass::$var;
MyClass::setVar(1);
echo MyClass::getVar(); //Outputs 1
You should be able to do this using a static member variable.
class foo {
private static $var;
public static setVar($value) {
self::$var = $value;
}
public static getVar() {
return self::$var;
}
}
$a = new foo;
$a::setVar('bar');
$b = new foo;
echo $b::getVar();
// should echo 'bar';
You should declare $var to be static:
A data member that is commonly
available to all objects of a class is
called a static member. Unlike regular
data members, static members share the
memory space between all objects of
the same class.
You can use static variables:
class AAA{
public static $var = 0;
function getvar() {
return AAA::$var;
}
}
AAA::$var = "test";
$a1 = new AAA();
var_dump($a1->getvar());
var_dump(AAA::$var);

Categories