PHP access an object of a class in another class - php

I'm trying to access an object of a class in another extended class.
class MainClass{
protected $theobject;
function __construct(){
$this->theobject = new AnotherClass();
}
}
class TheClass extends MainClass{
function AnotherFunction(){
$this->theobject->SomeFunction();
}
}
I'm getting an error on $this->theobject->AnotherFunction(). The error is "Call to a member function SomeFunction() on a non-object".
But this works fine:
class TheClass{
protected $theobject;
function SoemFunction(){
$this->theobject = new AnotherClass();
$this->theobject->SomeFunction();
}
}
Is it even legal to do this in PHP?
Pretty much all the code: http://3v4l.org/MaXe6
When i var_dump the $this->theobject in TheClass is comes back as NULL.

class TheClass extends MainClass{
function __construct(){
parent::__construct();
}
function SoemFunction(){
$this->theobject->SomeFunction();
}
}
Do the construct method when init the child class.

Related

class another class method in another class in php

I created a class named function and calling function method in another class named test. But it shows me an error -
PHP Fatal error: Call to a member function
my function class's object name is $obj = new functions();
when i call user method which is written in function class from test class I am getting an error.
class className {
function users(){
$users = "Demo User"; return $user;
}
}
$obj = new functions();
you mean this:
you can extends
class className extends test{
function __construct(){
here you add
}
}

Interacting with multiple PHP classes

I am learning on PHP classes. I can write a basic PHP classes but having trouble when accessing variables and functions from other classes.
My first class
Class First_class
{
public $options;
function __construct()
{
$this->options = get_option('theme_options'); //wordpress fn
}
function my_function() {
add_menu_page(...)
}
}
Class Second_class
{
public $first_class_object;
function __construct()
{
$this->first_class_object = new First_class();
//Trying to access $this->options here
}
}
new Second_class();
Class Third_class
{
public $first_class_object;
function __construct()
{
$this->first_class_object = new First_class();
//Trying to access $this->options here
}
}
new Third_class();
I am trying to access public variables from Class First_class using $first_class_object = new First_class(); in the second class and third classes. You see when I initiate new First Class in other classes add_menu_page() triggers two times. I am not sure how to access the variables and functions properly. May be constructors?
Sorry may be I am something wrong here?
I am looking for an experts suggestion.
Extend your classes.
An example is below using your code as an example. 3v4l demo is here and php extends docs are here.
<?php
class FirstClass
{
public $options;
public function __construct()
{
$this->options = ['yay']; //get_option('theme_options'); //wordpress fn
}
public function myFunction()
{
//
}
}
class SecondClass extends FirstClass
{
}
class ThirdClass extends FirstClass
{
}
$secondClass = new SecondClass();
var_dump($secondClass->options);
$thirdClass = new ThirdClass();
var_dump($thirdClass->options);
if you try accessing other class Properties, you could use className::propertyName
OR
if you wanna access properties within the class you can use self:: or $this.
know more about accessing properties and traverses under classes.
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php
try extend class
class Third_class extends First_class
{
public $first_class_object;
function __construct()
{
$this->first_class_object = new First_class();
//Trying to access $this->options here
}
}
or use this construction for access to you class
$this->first_class_object->option

Difficulty referring to class function

I need to call a function someFunction() how do I refer to it when it is in the following class structures?
abstract class A
{
protected $session;
protected $model;
public function __construct()
{
$session = new classSession;
$model = new classModel;
}
}
class classModel
{
$this->session->someFunction();
}
I've tried using $this->session->someFunction() but it does not work!
Firstly, you need to put it within the context of a function - code cannot exist on its own within a class body. Secondly, for anything in classModel to access $session, classModel has to extend class A. You end up with something like this:
class classModel extends A
{
public function foo()
{
$this->session->someFunction();
}
}
So for $this->session->someFunction(); to execute, you'd do this:
$model = new classModel();
$model->foo();

Inheriting instance in PHP

I try to call getTest() function inside Child class.
First I initialize instance of Ext class so I assume main property $ext should now contain it. But Child class does not inherit it and obtained error message is:
Call to a member function getTest() on a non-object
Where is the issue?
<?php
$A = new Main;
class Main
{
protected $ext = null;
function __construct()
{
$this->ext= new Ext();
new Child;
}
}
class Child extends Main
{
function __construct()
{
echo $this->ext->getTest();
}
}
class Ext extends Main
{
public function getTest()
{
return "cool";
}
}
?>
I know that to solve it other way I can use:
class Child
{
private $Main;
function __construct( &$Main ) { ... }
}
but I would like to understand why that does not work..
At the moment on construct the object the atribute don't have a value yet.
You need call to the parent constructor before use the attribute
function __construct()
{
parent::__construct();
echo $this->ext->getTest();
}

Is there a way to assign a member variable to an object of a class without a constructor?

I want to do something like the following:
class SomeOtherClass {}
class Test
{
public $member = new SomeOtherClass();
}
The only problem is that I do not want to use a constructor, because the 'Test' class should extend another class and should not override the constructor.
Is this actually possible in PHP?
You can extend parent constructor in Test like this:
class Test extends SomeClass
{
public $member;
function __construct() {
$this->member = new SomeOtherClass();
parent::__construct();
}
}

Categories