I have the simplest bit of code :
Interface
interface iCrudRepository{
public function Create($id);
public function Read($id);
public function Update($id);
public function Delete($id);
}
Parent
class Repository
{
function __construct()
{
echo "SHOULD NOT BE CALLED AUTOMATICALLY";
}
}
Class
require_once(__DIR__.'/../injection/bootstrap.php');
class Admin extends Repository implements iCrudRepository
{
function Create($id)
{
}
function Read($id)
{
}
function Update($id)
{
}
function Delete($id)
{
}
}
$admin = new Admin();
$admin->Create("Something");
The bootstrap class autoloads my classes via the spl_autoload_register function. Since in the Admin class I don't call the parent constructor, it shouldn't execute what is in the parent's constructor right?
The Output
SHOULD NOT BE CALLED AUTOMATICALLY
Probably missing something obvious here but can't quite figure out why it is called.
Docs state:
Parent constructors are not called implicitly if the child class
defines a constructor.
So you have to do this in order to prevent what you are seeing:
class Admin extends Repository implements iCrudRepository
{
public function __construct()
{
}
function Create($id)
{
}
function Read($id)
{
}
function Update($id)
{
}
function Delete($id)
{
}
}
Related
I want to call a function of main class to a child class. But here child class is not directly connected with parent class . Please see my code below.
main-class.php
class Main_Class {
public function __construct() {
$this->include_files();
}
public function include_files(){
include_once('first_class.php');
}
public function my_parent_function(){
echo 'this is my super parent_function';
}
}
first_class.php
class First_class {
public function __construct() {
$this->include_files_1();
}
public function include_files_1(){
include_once('my_abstaract_class.php');
include_once('second_class.php');
}
}
my_abstaract_class.php
abstract class My_abstaract_class {
...
}
second_class.php
class Second_class extends My_abstaract_class {
public function __construct() {
$this->my_child_function();
}
public function my_child_function(){
$this->my_parent_function();
}
}
Here i want to call my_parent_function() inside second_class.php. How can I do this? When I tried $this->my_parent_function(); it's not working.
Suppose I have the following :
<?php
class Final extends Intermediate {
public function final_level() {
$this->low_level();
$this->inter_level();
}
}
class Intermediate extends Lib1 {
public function inter_level() {
$this->low_level();
}
}
class Lib1 {
public function low_level1();
public function low_level2();
}
class Lib2 {
public function low_level1();
public function low_level2();
}
I would like to change the Intermediate class to extend Lib1 or Lib2, depending on some conditions, without duplicating Intermediate and Final code content.
All low_level functions are the same for both Lib.
In the end, I would like to have a Final1 class that use Lib1 (and Final2 that use Lib2).
How could I achieve this ?
You cannot achieve this via inheritance but you can via delegation
With this approach you delegate the implementation of some methods to a 'delegate' object rather than a base class.
Here it is an example:
<?php
class Final extends Intermediate {
public function __construct(Lib delegate) {
parent::__construct(delegate);
}
public function final_level() {
$this->low_level();
$this->inter_level();
}
}
class Intermediate implements Lib { //here you implement an interface rather than extending a class
private Lib delegate;
public function __construct(Lib delegate) {
$this->delegate = delegate;
}
public function inter_level() {
$this->low_level();
}
public function low_level() {
//delegate!
$this->delegate->low_level();
}
}
class Lib1 implements Lib{
public function low_level(); //implementation #1
}
class Lib2 implements Lib {
public function low_level(); //implementation #2
}
interface Lib {
public function low_level();
}
now you can create your final1 and final2 object in this way:
$final1 = new Final(new Lib1());
$final2 = new Final(new Lib2());
or, if you prefer, you can create the Final1 and Final2 classes extending from Final:
class Final1 extends Final {
public function __construct()
{
parent::__construct(new Lib1());
}
}
class Final2 extends Final {
public function __construct()
{
parent::__construct(new Lib2());
}
}
$final1 = new Final1();
$final2 = new Final2();
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 BaseController extends Controller
{
protected $foo;
public function __construct()
{
$this->foo = '123';
}
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
Above is the BaseController and I want to declare foo as 123, but can I get the foo variable in the controller which I have extended from this basecontroller, can you help?
public function detail($action)
{
return $this->foo;
}
As per docs:
http://php.net/manual/en/language.oop5.decon.php
Note: Parent constructors are not called implicitly if the child class
defines a constructor. In order to run a parent constructor, a call to
parent::__construct() within the child constructor is required.
As you are doing some work in your parent class constructor, you must invoke it directly in your subclass too (even this would be to only thing you do in child's constructor). I.e.:
class ChildController extends BaseController
{
public function __construct() {
parent::__construct();
}
...
When you extend the controller, I imagine that you're currently doing this:
<?php
class NewController extends BaseController
{
public function __construct()
{
// Do something here.
}
public function detail($action)
{
return $this->foo;
}
}
You see how the __construct method is being overwritten. You can easily fix this by adding parent::__construct() to the beginning of the method, so you'll have this:
<?php
class NewController extends BaseController
{
public function __construct()
{
parent::__construct();
// Do something here.
}
public function detail($action)
{
return $this->foo;
}
}
To be specific, i have a two class Request and Utils,
class Request
{
public function __construct()
{
//constructor method
}
public function request()
{
$utils=new Utils;
$consolidated_errors=$utils->array_remove_empty($all_params_error);
}
public function process()
{
$utils=new Utils;
$consolidated_errors=$utils->another_method($all_params_error);
}
}
And class Utils,
class Utils
{
public function __construct()
{
//constructor method
}
public function array_remove_empty()
{
//returns a variable.
}
public function another_method()
{
//returns a variable.
}
}
you can see that i am initializing the class two times in request class , and my question is that any way initializing the class globally and using through out the class?
You are looking for Singleton pattern
Following demonstrate very basic Singleton example for your class
public class Utils {
private static Utils uniqInstance;
private Utils() {
}
public static synchronized Utils getInstance() {
if (uniqInstance == null) {
uniqInstance = new Utils();
}
return uniqInstance;
}
// other useful methods here
}
get the instance using static-factory pattern
The above code does not look like Java to me, but anyway,
You could create the class at a class level private Utils myUtuils = new Utils ();
or
have the class as a static class and then just use it directly in your method
public function process()
{
consolidated_errors= Utils.another_method($all_params_error);
}
}