Calling static method inside constructors php - php

public function __construct($docID) {
self::getTranscriptionInfo($docID);
}
protected static function getTranscriptionInfo($docID) {
$cache_dir = $this->cache_dir;
}
I get error"Uncaught Error: Using $this when not in object context in". How to fix it?
I want to make getTranscriptionInfo() static
I want to call getTranscriptionInfo() inside construct.

Related

error using $this in non-object context.intelephense(1030) and Non-static method cannot be called statically

I have a class with the structure below. And Im trying to call a method of the class with "$this->getTopProds($prodsInfo)" however Im getting an error:
"Cannot use '$this' in non-object context.intelephense(1030)"
And on the page the error is "Non-static method App\JsonResponse\Prod\ProdRender:: getTopProds() cannot be called statically".
Do you know what can be the issue?
class ProdRender
{
public static function hotel(array $prodsInfo): array
{
dd($this->getTopProds($prodsInfo));
}
private function getTopProds(array $prodsInfo)
{
//
}
}
No, we can't use this keyword inside a static method. “this” refers to the current instance of the class. But if we define a method as static, the class instance will not have access to it
To keep using the other method call inside the static method you need to change the second method getTopProds to be static too, and call it with self
self::getTopProds($prodsInfo)
In case you need it try this:
<?php
class ProdRender
{
public function hotel(array $prodsInfo)
{
return $this->getTopProds($prodsInfo);
}
private function getTopProds(array $prodsInfo)
{
return $prodsInfo;
}
}
$ho = new ProdRender();
$response = $ho->hotel(["fadf","ceec"]);
var_dump($response);

Accessing parent property inside parent class throws error - using $this when not in object context

Parent Class
class admarvel_generic_network
{
protected $attributeSettings;
public function __construct()
{
$this->attributeSettings = "something";
}
public static function parentGetAd()
{
print_r($this->attributeSettings); //throws FATAL ERROR Using $this when not in object context
}
}
Child Class - Initiating object of same class within static function
class Agencies_selectablemedia_test extends admarvel_generic_network
{
public static function getAd($frengoAdParams)
{
$adnw = new Agencies_selectablemedia_test();
$ad = $adnw->parentGetAd();
return $ad;
}
}
//Entry point
$ad_contents = Agencies_selectablemedia_test::getAd($params);
echo $ad_contents;
I get a fatal error, as highlighted in the code above.
I checked that if I make the following changes in child and parent class -
Parent Class
public static function parentGetAd($obj)
{
print_r($obj->attributeSettings); //this works
}
Child Class
public static function getAd($frengoAdParams)
{
$adnw = new Agencies_selectablemedia_test();
$ad = admarvel_generic_network::parentGetAd($adnw); //used scope resolution operator and passed object as parameter.
return $ad;
}
Could someone explain the above? I would like to understand why I cannot use $this->attributeSettings in the parent class's parentGetAd() function.
The reason why you cannot access $this->attributeSettings is that your are in a static method. So you aren't in the context of an object.
public static function parentGetAd($obj)
If you are declaring the method like this
public function parentGetAd($obj) {
}
you should be able to access $this->attributeSettings.

Accessing a public/private function inside a static function?

Due to the fact that you can not use $this-> inside a static functio, how are you supposed to access regular functions inside a static?
private function hey()
{
return 'hello';
}
public final static function get()
{
return $this->hey();
}
This throws an error, because you can't use $this-> inside a static.
private function hey()
{
return 'hello';
}
public final static function get()
{
return self::hey();
}
This throws the following error:
Non-static method Vote::get() should not be called statically
How can you access regular methods inside a static method? In the same class*
Static methods can only invoke other static methods in a class. If you want to access a non-static member, then the method itself must be non-static.
Alternatively, you could pass in an object instance into the static method and access its members. As the static method is declared in the same class as the static members you're interested in, it should still work because of how visibility works in PHP
class Foo {
private function bar () {
return get_class ($this);
}
static public function baz (Foo $quux) {
return $quux -> bar ();
}
}
Do note though, that just because you can do this, it's questionable whether you should. This kind of code breaks good object-oriented programming practice.
You can either provide a reference to an instance to the static method:
class My {
protected myProtected() {
// do something
}
public myPublic() {
// do something
}
public static myStatic(My $obj) {
$obj->myProtected(); // can access protected/private members
$obj->myPublic();
}
}
$something = new My;
// A static method call:
My::myStatic($something);
// A member function call:
$something->myPublic();
As shown above, static methods can access private and protected members (properties and methods) on objects of the class they are a member of.
Alternatively you can use the singleton pattern (evaluate this option) if you only ever need one instance.
I solved this by creating an object of the concerning class inside a static method. This way i can expose some specific public functions as static function as well. Other public functions can only be used like $object->public_function();
A small code example:
class Person
{
public static function sayHi()
{
return (new Person())->saySomething("hi");
}
public function saySomething($text)
{
echo($text);
}
private function smile()
{
# dome some smile logic
}
}
This way you can only say hi if using the static function but you can also say other things when creating an object like $peter->saySomething('hi');.
Public functions can also use the private functions this way.
Note that i'm not sure if this is best practice but it works in my situation where i want to be able to generate a token without creating an object by hand each time.
I can for example simply issue or verify a token string like TokenManager::getTokenToken(); or TokenManager::verifyToken("TokenHere"); but i can also issue a token object like $manager = new TokenGenerator(); so i can use functionality as $manager->createToken();, $manager->updateToken(updateObjectHere); or $manager->storeToken();

Static methods requiring var

Ok, i'm stuck on this, why don't i get what i need?
class config
{
private $config;
# Load configurations
public function __construct()
{
loadConfig('site'); // load a file with $cf in it
loadConfig('database'); // load another file with $cf in it
$this->config = $cf; // $cf is an array
unset($cf);
}
# Get a configuration
public static function get($tag, $name)
{
return $this->config[$tag][$name];
}
}
I'm getting this:
Fatal error: Using $this when not in object context in [this file] on line 22 [return $this->config[$tag][$name];]
And i need to call the method in this way: config::get()...
public static function get
need to be
public function get
You can't use $this in static methods.
EDITED
I could do this, but I'm not sure if it's the best design for you.
class config
{
static private $config = null;
# Load configurations
private static function loadConfig()
{
if(null === self::$config)
{
loadConfig('site'); // load a file with $cf in it
loadConfig('database'); // load another file with $cf in it
self::$config = $cf; // $cf is an array
}
}
# Get a configuration
public static function get($tag, $name)
{
self::loadConfig();
return self::$config[$tag][$name];
}
}
The problem is that you're Using $this when not in object context... Declaring a method as static removes the possibility to use the $this-reference inside the method.
There is no $this reference inside static methods as they belong to the class. Static methods can only access static members, so if it is important that get() is a static method, make $this->config a static member and return self::$config[$tag][$name]. However, the static keyword makes methods accessible without an instance of the class and I'd advise either making get() non-static, or making the class a singleton (depending on how you wish to use it).

PHP static classes problem

<?php
class jokz {
static public $val='123';
static public function xxx() {
jokz2(self);
}
}
function jokz2($obj) {
echo $obj::$val;
}
jokz::xxx();
?>
it returns fatal error, cause the class "self" couldn't be found...
so.. how can i make that work?
passing a parameter by reference in function also don't work
function jokz2(&$obj) {
echo $obj::$val;
}
There is no object to pass (self refers to static class). You can pass the name of the class and call it that way (using call_user_func) or instantiate an object and pass $this
You would have to use $this to use the current object.

Categories