how to change non static variable to static method in php - 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();

Related

Is there any way I can change class variable, which would affect all instances created after?

class foo
{
public $bar = 1;
}
#foo.bar = 2; //change class variable and affect all after
$a = new foo();
$a->bar = 2;//avoid DRY
$b = new foo();
$b->bar = 2;//avoid DRY
echo $a->bar;
echo $b->bar;
Is there any way I can change class variable, which would affect all instances created after?
I want to change one default value, but I don't want to keep repeating every time I instantiate a new object.
You'd need to make the variable static.
class foo
{
public static $bar = 1;
}
$a = new foo();
$a::$bar = 2;
$b = new foo();
echo $b::$bar; // 2
You can also do Foo::$bar = 2; to set the variable for all instances.
Having a static property works great if the constructor should be called each time. If you're attempting to chain invocations, however, you might use clone instead.
class Foo
{
public $bar = 1;
public function bar(?int $bar = null): Foo
{
$this->bar = $bar ?? $this->bar;
return $this;
}
public function copy(?int $bar = null): Foo
{
return $this->fork()->bar($bar ?? $this->bar);
}
public function fork(): Foo
{
return clone $this;
}
}
$a = new foo();
$a2 = $a->copy();
$b = $a->copy(2);
$b2 = $a->copy();
$c = $b->copy(3);
$c2 = $b->copy();
https://3v4l.org/Fevli
Note, there is also a __clone() magic method that allows you to configure the actual cloned properties that go along (e.g., reset or increment, etc).
Here's method using static::with() methodology, which might fit best for the DRY approach of preconfiguration.
class Foo
{
public const DEFAULT_BAR = 5;
public $bar = FOO::DEFAULT_BAR;
public function bar(?int $bar = null): Foo
{
$this->bar = $bar ?? $this->bar;
return $this;
}
static public function withBar(?int $bar = null): Foo
{
return (new self())->bar($bar ?? Foo::DEFAULT_BAR);
}
public function fromBar(?int $bar = null): Foo
{
return (new self())->bar($bar ?? $this->bar);
}
public function fork(): Foo
{
return clone $this;
}
}
$a = Foo::withBar();
$b = Foo::withBar(4);
$b2 = $b->fromBar();
$c = Foo::withBar();
https://3v4l.org/qI8SY

How can I cast class for my object for PHP?

I have object $obj (class A).
Can I convert class for $obj to B?
Perhaps there is another way.
Example:
class A
{
public $AProp = 1;
public function &Convert($ATypeName)
{
// HOW?
return $this;
}
}
class B extends A
{
public $BProp = 2;
}
$obj=new A();
$obj->Convert("B");
print_r($obj->BProp);
I wrote next solution, but it is no good.
(It looks like your post is mostly code; I add some more details)
class A
{
public $AProp = 1;
public function &Convert($ATypeName)
{
$Result = new $ATypeName; // Create new object
$Result->AProp = $this->AProp; // Copy params...
return $Result;
}
}
class B extends A
{
public $BProp = 2;
}
$obj = new A();
$obj->AProp = 3;
$obj = $obj->Convert("B");
print_r($obj);
u are trying to use c++-way of class extending.
the php-way is smth like this:
<?php
class A
{
protected $prop = 1;
public function getProp()
{
return $this->prop;
}
}
class B extends A
{
protected $prop = 2;
}
$obj=new A();
print_r($obj->getProp());
$obj=new B();
print_r($obj->getProp());
also take a look on late static bindings - http://php.net/manual/en/language.oop5.late-static-bindings.php
<?php
class A
{
public static $prop = 1;
public function getProp()
{
return static::$prop;
}
}
class B extends A
{
public static $prop = 2;
}
$obj=new A();
print_r($obj->getProp());
$obj=new B();
print_r($obj->getProp());
This is the only way:
$obj = new B();
Since inheritance is used you can access all class A func and var, like $this->Aprop

Get property from another class function?

<?php
//file classA.php
class A {
private $B;
public $data;
public function __construct(){
$this->B = new B();
}
public function readA(){
$this->data = $this->B->readB();
print $this->data;
}
public function sendB(){
return "WORD";
}
}
//file classB.php
class B {
private $A;
public function __construct(){
$this->A = new A();
}
public function readB(){
return $this->A->sendB();
}
}
require_once .... classA.php
requier_once .... classB.php
$classA = new A();
$classA->readA();
I wanna use classes with multiple dependences.
Can't use instance methods or extends classes.
How can i get function result and send it back to the same class from another?
Your problem is an endless loop of object creation:
When you create class A it'll create an Object of Class B, that will create another object of Class A again, that will create an Object of Class B,....
-> Memory Error
So if you get rid of $this->B = new B(); in __construct() of class A it'll work with that change:
You had:
// in class B
public function readB(){
return $this->sendB();
}
// it needs to be:
public function readB(){
return $this->A->sendB();
}
Complete working code:
EDIT: Now with readA() in class A, but creation of class B out of constructor.
<?php
class A {
private $B;
public $data;
public function __construct(){
// $this->B = new B();
}
public function readA(){
$this->B = new B();
$this->data = $this->B->readB();
print $this->data;
}
public function sendB(){
return "WORD";
}
}
//file classB.php
class B {
private $A;
public function __construct(){
$this->A = new A();
}
public function readB(){
return $this->A->sendB();
}
}
$B = new B();
echo $B->readB();
$A = new A();
echo $A->readA();
?>

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

Categories