• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a protected function from another class

#1
Class A
PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;
class 
extends Controller
{
    protected function 
a()
    {
        return 
'a';

    }


Class B:
PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;
use 
App\Controllers\A;
class 
Test extends Controller
{
    private 
$a;
    function 
__construct()
    {
        
$this->a= new A();
    }
    function 
b(){
        
$b $this->a->a();
        return 
$a;
    }


Error
Call to protected method...

How can I get the 'a' in Class B
Reply

#2
You don't get access to a() just because you extend from the same base class. You should probably look into using traits instead.
Reply

#3
Read this: PHP.NET - Visibility
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
(08-03-2020, 02:54 AM)tgix Wrote: You don't get access to a() just because you extend from the same base class. You should probably look into using traits instead.

thanks
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.