Can't call function of another class using OOP - php

I'm a bit rusty with php, I want to know how I can call the function login available in class2, inside class1. This is the example:
<?php
require_once("property2.php");
class Class1
{
public function __construct()
{
$cls2 = new Class2()
}
public function method1()
{
$cls2->login() //cl2 is undefined
}
} ..
//this is the function
...
class Class2
{
public function __construct()
{
}
//This is the function to call
public function login()
{
//Some stuff
}
} ...
Now PHPSTORM say that the variable cls2 is undefined. What I did wrong?

When you are setting your variable youre not setting it as a class property. Define a private variable inside your class, and "set it and get it" using the $this keyword.
class Class1 {
private $cls2;
public function __construct() {
$this->cls2 = new Class2();
}
public function method1() {
$this->cls2->login();
}
}
Another way to achieve this is to use Inheritance, where one class is considered a "parent" class. You would achieve this by using extends
class Class1 {
public function __construct() {
//Some stuff
}
public function login() {
//Some stuff
}
}
class Class2 extends Class1 {
public function __construct() {
parent::__construct();
}
public function method1() {
$this->login();
}
}

class Class1
{
public function __construct()
{
$cls2 = new Class2();
}
public function method1()
{
$cls2->login() //cl2 is undefined
}
}
When you create Class1 and call $cls2 = new Class2();, $cls2 exists only locally. You have to make it a class property:
class Class1
{
public $cls2;
public function __construct()
{
$this->cls2 = new Class2();
}
public function method1()
{
$this->cls2->login();
}
}
And then you'll be able to access it using $this keyword.
Also please watch for semicolons.

Related

How to call a function of child class inside a function of parent class

I want to call a function defined in child class from a function defined in the parent class for some logic. I am getting error of undefined function. How can I call this function. Here is my sample code.
<?php
class First
{
public function __construct()
{
echo "First class is initiated.";
}
public function call_child()
{
$this->get_ouput();
}
}
class Second extends First
{
public function __construct()
{
parent::__construct();
}
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
?>
The issue is spelling mistake in your function call name
<?php
class First
{
public function __construct()
{
echo "First class is initiated.";
}
public function call_child()
{
$this->get_output(); //update the name here
}
}
class Second extends First
{
public function __construct()
{
parent::__construct();
}
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
?>
There is a typo when calling the get_output method. However you should define the First class and get_output method as abstract in order to be extended/implemented by the other classes:
abstract class First
{
public function __construct()
{
echo "First class is initiated.";
}
abstract public function get_output();
public function call_child()
{
$this->get_output();
}
}
class Second extends First
{
public function get_output()
{
echo "Here is your output";
}
}
$obj = new Second();
$obj->call_child();
Note: the Second class may not define the constructor if it only calls the parent constructor.

How do you access a child method

How do you access a child method eg.?
class A
{
public function Start()
{
// Somehow call Run method on the B class that is inheriting this class
}
}
class B extends A
{
public function Run()
{
...
}
}
$b = new B();
$b->Start(); // Which then should call Run method
Class A should not try to call any methods that it itself does not define. This will work just fine for your scenario:
class A {
public function Start() {
$this->Run();
}
}
However, it will fail terribly should you actually do this:
$a = new A;
$a->Start();
What you're trying to do here sounds very much like a use case for abstract classes:
abstract class A {
public function Start() {
$this->Run();
}
abstract function Run();
}
class B extends A {
public function Run() {
...
}
}
The abstract declaration will precisely prevent you from shooting your own foot by trying to instantiate and Start A without extending and defining required methods.
If B is inherited from A then B will be like:
class B extends A
{
public function Start()
{
...
}
public function Run()
{
...
}
}
So as Run() and Start() are in the same class, we can call Run() in Start() directly.
public function Start()
{
Run();
}

PHP class property reference to an object

class Hello {
public function hi() {
echo "Hello, hi!\n";
}
}
class ParentClass {
public $obj;
public function __construct() {
$this->obj = new Hello;
}
}
class Test extends ParentClass {
public function __construct() {
$this->obj->hi();
}
}
$temp = new Test;
The error message I get is "Call to a member function hi() on a non-object". $obj should be referencing to an instance of the class "Hello", but it obviously is not - what am I doing wrong?
You are defining __construct() in your Test class but not calling the parent constructor. If you want the parent constructor to execute, you need to explicitly specify so. Add a call to ParentClass constructor in in Test class constructor.
class Test extends ParentClass {
public function __construct() {
parent::__construct();
$this->obj->hi();
}
}
Also as #Tasos Bitsios pointed in his comment you also need to update your ParentClass constructor as follows:
class ParentClass {
public $obj;
public function __construct() {
$this->obj = new Hello; // Use $this->obj and not just $obj.
}
}
You need call to parent constructor:
class Test extends ParentClass {
public function __construct() {
parent::__construct();
$this->obj->hi();
}
}

Call parent static method in php

I have a base class A:
class A {
public static function a() {
...
}
public static function b() {
...
}
}
and an extended class B
class B extends A {
public static function a() {
...
}
public static function c() {
...
}
}
I would like to be able to call all the methods using B::
How would I call A::b, using B::?
You should be able to accomplish this as easily as:
class B extends A {
public static function a() {
parent::a();
}
}
See the docs

Multiple time execution bypass

I have the follow (sample) code:
class Foo {
public function __construct() {
$this->bar = new Bar();
$this->baz = new Bazr();
}
}
class Bar extends Foo {
public function __construct() {
parent::__construct();
$baz = $this->baz->alert();
}
}
class Baz extends Foo {
public function __construct() {
parent::__construct();
}
public function alert() {
echo('Hello!');
}
}
new Foo();
Which will generate an Fatal error: Maximum function nesting level of '100' reached, aborting!. What I want is, exactly this, ofcourse with different code and no errors.
What I want is one way to know when my instance is already created and not allow more instances of same object, avoiding circular reference.
Learned about Singletom, but, nothing worked. Have any idea?
The constructor of Foo calls the constructor of Bar which calls the constructor of Foo which calls the constructor of Bar... you get the idea. After 100 times it tells you it's dizzy. Don't create a circular reference like that.
I do not quite understand why you need all this? If only because to get methods of Bar/Baz in class Foo, then you can use:
class Foo {
public function __construct() {
Bar::init();
}
}
class Bar extends Foo {
public function __construct() {
parent::__construct();
}
public function init() {
Baz::alert();
}
}
class Baz extends Foo {
public function __construct() {
parent::__construct();
}
public function alert() {
echo 'Hello!';
}
}
new Foo();
Implement dependence Injection is the solution:
class Foo {
public function __construct() {
$this->bar = new Bar(new Baz());
}
}
class Bar {
public function __construct($baz = null) {
$this->baz = $baz;
$this->baz->alert();
}
}
class Baz {
public function __construct() {
}
public function alert() {
echo('Hello!');
}
}
new Foo();

Categories