Cannot create object in class PHP - 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();
}

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

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

Does this smell if I cant decide which descendant should I use?

abstract class X
{
private $v;
protected function setV($v)
{
$this->v = $v;
}
public function getV()
{
return $v;
}
}
class A extends X
{
public function doIt()
{
parent::setV(1);
}
}
class B extends X
{
public function doIt()
{
parent::setV(2);
}
}
$a = new A();
$a->doIt();
$b = new A();
$b->doIt();
but if I want to use getV(), I can both call
$a->getV() and $b->getV()
which sounds silly. Which one to use? To be honest, I would like to see something like that:
X::getV();
which is not possible, an instance must be exists/
It depends on "what do you want". Firstable, it's possible to use X::getV() method, but you need to make v member and getV method static, as shown below.
<?php
abstract class X
{
private static $v;
protected static function setV($v)
{
self::$v = $v;
}
public static function getV()
{
return self::$v;
}
}
class A extends X
{
public function doIt()
{
self::setV(2);
}
}
class B extends X
{
public function doIt()
{
self::setV(1);
}
}
$a = new A();
$a->doIt();
echo X::getV();
// prints 2
// but be aware, that ANY instance of X children class will change the same X::$v value
$b = new B();
$b->doIt();
echo X::getV();
// prints 1
Static members (like X::$v) are stored only once, they are "binded" to the class, not to the instance of this class.
<?php
class Foo
{
public static $v = 5;
}
$instance1 = new Foo();
$instance2 = new Foo();
echo Foo::$v;
echo $instance1::$v;
echo $instance2::$v;
// prints 5, 5, 5
$instance1::$v = 10;
echo Foo::$v;
echo $instance1::$v;
echo $instance2::$v;
// prints 10, 10, 10

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

How to access a variable that inherits to a php class that we called?

we have two class A & B:
class A{
var $settings;
function getinfo(){
$settings['domain']="mydomain";
$settings['pass']="1234";
return $settings;
}
}
class B extends A{
$ads = A::getinfo();
function makeurl(){
return "template/".$ads['domain'];
}
}
now i have an instance of B in my page, but i need "pass" , maybe some code like this:
$theme=new B();
$mypass = $theme->A->getinfo;
echo $mypass[$pass];
I know this code is full of faults , but i could not write a better one. is there any solution to access to password without making an instance of A?
Yes. It is as simple as this:
$theme = new B();
$mypass = $theme->getinfo();
echo $mypass['pass'];
You can also improve your classes a bit:
class A
{
var $settings;
function getinfo()
{
$this->settings['domain'] = "mydomain";
$this->settings['pass'] = "1234";
return $this->settings;
}
}
class B extends A
{
function makeurl()
{
$this->getinfo();
return 'template/' . $this->settings['domain'];
}
}
Why not call the settings variable in A from the B instance since B is a subclass of A?
Try this code:
<?php
class A
{
var $settings;
function getinfo()
{
$settings['domain'] = "mydomain";
$settings['pass'] = "1234";
return $settings;
}
}
class B extends A
{
function makeurl()
{
$ads = $this->getinfo();
return "template/" . $ads['domain'];
}
}
$theme=new B();
$mypass = $theme->getinfo();
echo $mypass['pass'];
What about making settings a public static variable in A? By making it a class variable you won't need an instance of A.
class A {
public static $settings;
// getter and setter methods here
}
// code elsewhere
echo A::$settings['pass'];
Also because your class B extends A it inherits the methods and properties, so you could call
$theme = new B();
$mySettings = $theme->GetInfo();
if B extends A, all protected and public members of A are inherited into B, so you can access them directly.
class A {
protected $foo;
public function __construct() { $this->foo = 1; }
}
class B extends A {
public function bar() {
echo $this->foo;
}
}
$b = B();
$b->bar();
If I understand you correctly, you're pretty close:
$theme=new B();
$settings = $theme->getinfo();
$mypass = $settings['pass'];
echo $mypass;

Categories