<?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.
Related
I'm dinamically instantiating a class, but I want to know if there's a way to call a method from this instance, thanks
Code:
if(class_exists($class_name)){
$class = new $class_name;
$class.method();
}
class foo implements bar {
public function method(): void {
echo "method called";
}
}
Expected Result:
Call the method from the object
Actual Result:
Error: Call to undefined function method()
In php use the arrow to call the class's function.
$class.method();
should be
$class->method();
alternatively if your method is declared as static you can use it like so
foo::method();
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);
<?php
class Popular
{
public static function getVideo()
{
return $this->parsing();
}
}
class Video
extends Popular
{
public static function parsing()
{
return 'trololo';
}
public static function block()
{
return parent::getVideo();
}
}
echo Video::block();
I should definitely call the class this way:
Video::block();
and not initialize it
$video = new Video();
echo $video->block()
Not this!
Video::block(); // Only this way <<
But:
Fatal error: Using $this when not in object context in myFile.php on line 6
How to call function "parsing" from the "Popular" Class?
Soooooooory for bad english
As your using a static method, you cant use $thiskeyword as that can only be used within objects, not classes.
When you use the new keyword, your creating and object from a class, if you have not used the new Keyword then $this would not be available as its not an Object
For your code to work, being static you would have to use the static keyowrd along with Scope Resolution Operator (::) as your method is within a parent class and its its not bounded, Use the static keyword to call the parent static method.
Example:
class Popular
{
public static function getVideo()
{
return static::parsing(); //Here
}
}
What does $this mean in PHP?
paamayim-nekudotayim - Scope Resolution
http://php.net/manual/en/language.oop5.static.php
change return $this->parsing(); to return self::parsing();
I am trying to figure out how to use a method within its own class. example:
class demoClass
{
function demoFunction1()
{
//function code here
}
function demoFunction2()
{
//call previously declared method
demoFunction1();
}
}
The only way that I have found to be working is when I create a new intsnace of the class within the method, and then call it. Example:
class demoClass
{
function demoFunction1()
{
//function code here
}
function demoFunction2()
{
$thisClassInstance = new demoClass();
//call previously declared method
$thisClassInstance->demoFunction1();
}
}
but that does not feel right... or is that the way?
any help?
thanks
$this-> inside of an object, or self:: in a static context (either for or from a static method).
You need to use $this to refer to the current object:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
So:
class demoClass
{
function demoFunction1()
{
//function code here
}
function demoFunction2()
{
// $this refers to the current object of this class
$this->demoFunction1();
}
}
Just use:
$this->demoFunction1();
Use $this keyword to refer to current class instance:
class demoClass
{
function demoFunction1()
{
//function code here
}
function demoFunction2()
{
$this->demoFunction1();
}
}
Use "$this" to refer to itself.
class demoClass
{
function demoFunction1()
{
//function code here
}
function demoFunction2()
{
//call previously declared method
$this->demoFunction1();
}
}
I have a question regarding static function in php.
let's assume that I have a class
class test {
public function sayHi() {
echo 'hi';
}
}
if I do test::sayHi(); it works without a problem.
class test {
public static function sayHi() {
echo 'hi';
}
}
test::sayHi(); works as well.
What are the differences between first class and second class?
What is special about a static function?
In the first class, sayHi() is actually an instance method which you are calling as a static method and you get away with it because sayHi() never refers to $this.
Static functions are associated with the class, not an instance of the class. As such, $this is not available from a static context ($this isn't pointing to any object).
Simply, static functions function independently of the class where they belong.
$this means, this is an object of this class. It does not apply to static functions.
class test {
public function sayHi($hi = "Hi") {
$this->hi = $hi;
return $this->hi;
}
}
class test1 {
public static function sayHi($hi) {
$hi = "Hi";
return $hi;
}
}
// Test
$mytest = new test();
print $mytest->sayHi('hello'); // returns 'hello'
print test1::sayHi('hello'); // returns 'Hi'
Entire difference is, you don't get $this supplied inside the static function. If you try to use $this, you'll get a Fatal error: Using $this when not in object context.
Well, okay, one other difference: an E_STRICT warning is generated by your first example.
Calling non-static methods statically generates an E_STRICT level warning.
In a nutshell, you don't have the object as $this in the second case, as
the static method is a function/method of the class not the object instance.
After trying examples (PHP 5.3.5), I found that in both cases of defining functions you can't use $this operator to work on class functions. So I couldn't find a difference in them yet. :(