Using $this when not in object context without the use of static methods

Ashish Patil

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?

Ravi Hirani

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 :-)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Static Methods ok to use when using parameters?

From Dev

Using an Entity Framework Context in static methods for an ObjectDataSource

From Java

Java: when to use static methods

From Dev

When to use static and class methods

From Dev

Using $this when not in object context?

From Dev

How to access the .class object in java without using the class name from a static context

From Dev

Using NON static class Methods Without reference

From Dev

Calling methods concept in PHP: static call versus object context

From Dev

Error: Using $this when not in object context

From Dev

Error Using $this when not in object context

From Dev

Using $this when not in object context php

From Dev

ERROR: using '$this' when not in object context

From Dev

Using $this when not in object context? PHP

From Dev

Using $this when not in object context php

From Dev

Using $this when not in object context in Codeigniter

From Dev

Getting error using $this when not in object context to all methods in a class except __construct() method

From Dev

When should I use static methods?

From Dev

PHP: When to use Traits and when to use static methods?

From Dev

Context "this" is not kept at object methods

From Dev

how can I use @Enclosed without making methods as static

From Dev

Using $this when not in object context - Laravel 4

From Dev

Using $this when not in object context with ReflectionFunction->invoke()

From Dev

Fatal error: Using $this when not in object context explanation?

From Dev

CodeIgniter "Using $this when not in object context" in function

From Dev

What causes this: Using $this when not in object context

From Dev

Fatal error: Using $this when not in object context

From Dev

Using $this when not in object context into the same class

From Dev

PHP : "Fatal error: Using $this when not in object context in"

From Dev

OOP Fatal error: Using $this when not in object context

Related Related

  1. 1

    Static Methods ok to use when using parameters?

  2. 2

    Using an Entity Framework Context in static methods for an ObjectDataSource

  3. 3

    Java: when to use static methods

  4. 4

    When to use static and class methods

  5. 5

    Using $this when not in object context?

  6. 6

    How to access the .class object in java without using the class name from a static context

  7. 7

    Using NON static class Methods Without reference

  8. 8

    Calling methods concept in PHP: static call versus object context

  9. 9

    Error: Using $this when not in object context

  10. 10

    Error Using $this when not in object context

  11. 11

    Using $this when not in object context php

  12. 12

    ERROR: using '$this' when not in object context

  13. 13

    Using $this when not in object context? PHP

  14. 14

    Using $this when not in object context php

  15. 15

    Using $this when not in object context in Codeigniter

  16. 16

    Getting error using $this when not in object context to all methods in a class except __construct() method

  17. 17

    When should I use static methods?

  18. 18

    PHP: When to use Traits and when to use static methods?

  19. 19

    Context "this" is not kept at object methods

  20. 20

    how can I use @Enclosed without making methods as static

  21. 21

    Using $this when not in object context - Laravel 4

  22. 22

    Using $this when not in object context with ReflectionFunction->invoke()

  23. 23

    Fatal error: Using $this when not in object context explanation?

  24. 24

    CodeIgniter "Using $this when not in object context" in function

  25. 25

    What causes this: Using $this when not in object context

  26. 26

    Fatal error: Using $this when not in object context

  27. 27

    Using $this when not in object context into the same class

  28. 28

    PHP : "Fatal error: Using $this when not in object context in"

  29. 29

    OOP Fatal error: Using $this when not in object context

HotTag

Archive