8591

PHP: Calling a private method from within a class dying badly

So this might sound a little convoluted. Fingers crossed I come across clearly.

I'm working in an MVC framework in PHP.

I load a controller /report/index which calls to a helper

<? class ReportController extends Controller { public function index() { $foo = MainReport::get_data($_REQUEST); } } ?>

Inside the helper

<? class MainReport extends foo { public function get_data($_REQUEST) { // do stuff return $stuff_done; } } ?>

It I run it like ^this all's well and good. Unfortunately, I want to run it like this:

<? class MainReport extends foo { private function do_stuff() { // do even better stuff here! return $better_stuff; } public function get_data($_REQUEST) { // do stuff $x = $this->do_stuff(); } } ?>

Unfortunately... when I try and call a private function from within a class that I've called from elsewhere... (whew, that's a mouthful) ... everything dies. Dies so very very badly that I don't even get an error.

It seems obvious to me that I'm having an incredibly dorky sort of syntax issue of some sort... but how do I correctly access private functions from within a class?

Maybe something like: self::do_stuff();

What about declaring and accessing private class variables?

private $bar = array();

Any help would be welcome.

Answer1:

You are calling your function from a static context,

MainReport::get_data($_REQUEST)

therefore $this does not exist while inside that function.

If you want to call another class function while inside a static context, you have to also call it statically.

i.e.

public function get_data($_REQUEST) { // do stuff $x = MainReport::do_stuff(); }

Alternatively, you can create an instance of your class in the original call and use the instance:

$myMainReport = new MainReport(); $myMainReport->get_data($_REQUEST);

Then your class code will work as expected

Answer2:

I've just found that self:: does work as well

if I want to have private class variables, I can declare and access them using

private static $foo

and

self::$foo = "foo";

additionally a private function can be accessed with

self::function_foo();

Recommend

  • Extracting data from a scatter plot on matplotlib
  • send an http request without XHR in an event handler
  • cURL Html output different from original page when rendered
  • Space in Postcode
  • mod_rewrite not passing querystring
  • How to add extra text to the tag in Joomla
  • How to access list of email accounts with cPanel API?
  • HTML code to upload images
  • Possible to “watch” both HAML and SASS at the same time?
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • Perl system calls when running as another user using sudo
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Build own AppleScript numerical error handling
  • Adding custom controls to a full screen movie
  • SVN: Merging two branches together
  • Load html files in TinyMce
  • JTable with a ScrollPane misbehaving
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?