2

The problem I'm having is a strange one, the log file says that the __construct method in my class called 'Upload' is private, BUT in reality it is Public! So the error makes no sense. This is my class code;

class Upload
{ 
    public $Errors; 
    public $ImageTypes;
    public $CDN_URL;
    public $UploadPath;
    private $MaxHeight;
    private $MaxWidth;
    private $MaxTokenLength;
    private $ForceMaxDimensions;
    private $MaxFileSize = 5; 

    public function __construct() 
    {
        include_once 'config.php';

        $this->ImageTypes = array('JPG', 'PNG');
        $this->Errors = array();

        # convert from megabytes to bytes
        $this->MaxFileSize = $this->MaxFileSize * 1048576;
    }
}

This is how I initiate the class;

include_once 'upload.php';
$Upload = new Upload;

This is the error from my log file;

PHP Fatal error:  Call to private Upload::__construct() from invalid context

I've looked at many answers online and non have helped me, so I'm very much hoping for the answer from Stackoverflow! Thanks for any help.

EDIT: Contents of config.php file

$ImageTypes = array('JPG', 'PNG');
$CDN_URL = 'CDN.php';
$UploadPath = 'uploads/';
$MaxHeight = 1000;
$MaxWidth = 1000;
$ForceMaxDimensions = TRUE;
$MaxFileSize = 5;// Mb
simon funiic
  • 31
  • 1
  • 5
  • 1
    Actually, you're not initializing a class with `include_once`. Where in your code do you have a statement like `$Upload = new Upload();`? – Michael Kunst Aug 12 '13 at 19:10
  • Yes sorry I do that, check my update (I originally copied the wrong line) – simon funiic Aug 12 '13 at 19:14
  • Just to be sure: This is the EXACT code of the Upload class - you're not extending from any other class or something like this? – Michael Kunst Aug 12 '13 at 19:20
  • @MichaelKunst Nope, this is the EXACT code – simon funiic Aug 12 '13 at 19:20
  • @Chris my PHP Version is 5.3.27 – simon funiic Aug 12 '13 at 19:21
  • 1
    what's in the config.php file? – Michael Kunst Aug 12 '13 at 19:21
  • 1
    @simonfuniic The reason that he asks... this code works as-is. Observe: http://codepad.viper-7.com/8bmyY3 -- that leads me to conclude you have other issues going on that are not obvious from the code you've posted. – Chris Baker Aug 12 '13 at 19:22
  • That is exactly what I was going to ask, @MichaelKunst :) – Chris Baker Aug 12 '13 at 19:22
  • @MichaelKunst The edit shows the contents of the config.php, nothing troubling in there that I can tell though..? – simon funiic Aug 12 '13 at 19:24
  • 2
    @simonfuniic -- do this, tell me what you get -- comment out your include line: `//include_once 'upload.php';`, then add this right below: `print 'c/e: '.class_exists('Upload');die();`. Report back with output. – Chris Baker Aug 12 '13 at 19:26
  • One would tend to believe you have more then one upload class... and this one is not included... might there be a namespace issue? – Wrikken Aug 12 '13 at 19:26
  • @Chris I did as you said and nothing was outputted, not even the c/e bit. – simon funiic Aug 12 '13 at 19:29
  • I do not have another classes running with these pages as this is a small test I'm playing about with, it is completely isolated – simon funiic Aug 12 '13 at 19:30
  • Little debugging, what happens when: - You comment out everything in the `__construct` function - You comment out everything in the `Upload` class - You comment out the line `$Upload = new Upload;`? – Michael Kunst Aug 12 '13 at 19:37
  • when I comment out the initiation of the class, the page loads but when parts of the code which depend on `$Upload` are reached there are other errors which is to be expected in its absence – simon funiic Aug 12 '13 at 19:51
  • Commenting out the code in `__Construct` leaves the same error – simon funiic Aug 12 '13 at 19:53
  • removing everything in the `upload` class still leaves the same error – simon funiic Aug 12 '13 at 19:56
  • http://www.php.net/manual/en/language.oop5.basic.php - $Upload = new Upload(); Are you missing brackets? – beiller Aug 12 '13 at 19:59
  • 1
    @beiller [no](http://stackoverflow.com/questions/3873111/instantiate-a-class-with-or-without-parentheses) – Michael Kunst Aug 12 '13 at 20:05
  • @beiller that doesn't change the error – simon funiic Aug 12 '13 at 20:05
  • Can you post your whole code somewhere? Maybe make a [phpFiddle](http://phpfiddle.org/) or something like that? I'm very curious about this error. – Michael Kunst Aug 12 '13 at 20:07
  • @MichaelKunst ok, but bear in mind it is still a work in progress – simon funiic Aug 12 '13 at 20:13
  • okay next question why does your error say private __construct(). It should be public as you show in your class definition. Is there some mix-up here? – beiller Aug 12 '13 at 20:26
  • @beiller Thanks what the whole conundrum is about :( – simon funiic Aug 12 '13 at 20:28
  • @MichaelKunst I've been told I cannot do that – simon funiic Aug 12 '13 at 20:29
  • can you remove the "public" keyword from the constructor function? – beiller Aug 12 '13 at 20:31
  • 1
    Maybe it's a stupid question. But are you sure that the content you posted above with the `Upload` class really is in the `upload.php` file you're including? Maybe you've got more than one upload.php files and you're including the wrong one? – Michael Kunst Aug 12 '13 at 20:34
  • @MichaelKunst Yep I just double checked – simon funiic Aug 12 '13 at 21:04
  • @beiller again no luck – simon funiic Aug 12 '13 at 21:04
  • As others here have mentioned, the error you are getting is simply not possible if you're showing us 100% of your code. As you saw in that paste I made (http://codepad.viper-7.com/1Wadz2) -- the code works. The error isn't going to appear out of nowhere! I think you need to seriously evaluate the files that you can't show us, check for includes, etc. You say that you've been told you "can't" post the code online, yet you already have... if this is all there is. Clearly, there is more. The problem is that there is an existing Upload class in that "secret" code, and it has a private constructor. – Chris Baker Aug 13 '13 at 14:23
  • You should turn on xdebug, and/or use `set_error_handler` to implement a custom error handler, you can output the stack trace. That should show which files are involved, and help you track down that other class. – Chris Baker Aug 13 '13 at 14:26
  • have you found the error yet? – Michael Kunst Aug 14 '13 at 14:21
  • One of the first things you should do when this kind of errors happen is change the name of your class. Upload is a nice name but could be already used. – Polak Nov 28 '15 at 04:52

1 Answers1

0

Based on your code posted, the mentioned error can't happen.

I propose to use CStatementTracer found in my answer to this question.

CStatementTracer uses PHP's tick-feature to trace each executed statement and dump this to a log file. That said, you need to enable CStatementTracer somewhere very early in your application.

Community
  • 1
  • 1
SteAp
  • 11,853
  • 10
  • 53
  • 88