2

I'm trying to manually decrypt my owncloud's files to test it, but I don't know the PHP language well.

The problem I'm facing is:

PHP Fatal Error: Using $this when not in object context

I looked around for some time, but all I came across was using $this wrongly along with static methods. But, there aren't any static methods in the files I'm editing.

There's a file 'script.php' where I'm calling another file's (crypt.php) methods.

script.php:

<?php 
namespace OCA\Files_Encryption\Crypto;
use OCA\Files_Encryption\Crypto\Crypt;
require_once 'crypt.php';

.
.
.

$decryptedUserKey = Crypt::decryptPrivateKey($encryptedUserKey, $userPassword);

.
.
.

Here's the other crypt.php file, where the fatal error occurs crypt.php

<?php
namespace OCA\Files_Encryption\Crypto;

class Crypt {

.
.
.

public function decryptedPrivateKey($privateKey, $password = '', $uid = '') {
    $header = $this->parseHeader($privateKey);
.
.
.
}

}

The last line of code throws the fatal error. Any ideas?

2 Answers 2

3

You can not use $this in static call. Because $this is refer current object and you haven't created any object for class Crypt.

Also you haven't declared decryptedPrivateKey method as static.

You can call class method by two ways. You can use Tom Wright's suggested way

(1) Call with object

$crypt = new Crypt(); // create class object
$decryptedUserKey = $crypt->decryptPrivateKey($encryptedUserKey, $userPassword); // call class method via object 

OR

(2) Call without object (static call)

a) You should define method as static.

b) You should use self keyword and call another static method,

public static function decryptedPrivateKey($privateKey, $password = '', $uid = '') {
    $header = self::parseHeader($privateKey);
}

public static function parseHeader() { // static parseHeader
  // stuff
}

In above case, parseHeader method must be static as well.

So you have two options:-

i) Either declare parseHeader method also static OR

ii) create object of current class and call non static method parseHeader

public static function decryptedPrivateKey($privateKey, $password = '', $uid = '') {
     $obj = new self(); // create object of current class
     $header = $obj->parseHeader($privateKey); // call method via object
}

public function parseHeader() { // Non static parseHeader
  // stuff
}

Hope it will help you :-)

5
  • Thanks, I have a better idea now. What about for calls like $this->abc->def(); ? Commented Jun 3, 2016 at 9:30
  • You are welcome :-) It is object chaining. see this link stackoverflow.com/questions/3724112/php-method-chaining Commented Jun 3, 2016 at 9:35
  • Thanks again! What I meant was, how should I modify such method chaining calls? I tried using the same method as you mentioned by declaring the methods as static and doing this: self::abc->def() but that did not work. Commented Jun 3, 2016 at 9:38
  • You can use self and call self::abc()->def() only if you will create class object in abc() method and return $this. then only it is possible. Commented Jun 3, 2016 at 9:50
  • Yeah, that makes sense. Thanks again @Ravi Hirani Commented Jun 3, 2016 at 9:54
3

You may not have defined decryptPrivateKey as static, but that is how you are using it.

It is then going forward and using $this when it isn't actually part of an instantiated object.

Try using this in script.php

$crypt = new Crypt();
$decryptedUserKey = $crypt->decryptPrivateKey($encryptedUserKey, $userPassword);

Not the answer you're looking for? Browse other questions tagged or ask your own question.