Basics of PHP class:passing value in class - php

I am debugging a site
I am having to work with classes which I am not that used to as of yet.
There is this class in this site that processes a $this but there does not seem to be any variable passed to the class.
the class is like this
class myclass extends otherclass{
function dosmthtomyclass{
print_r($this);
}
}
function dosmttomyclass prints an array.
there are bunch of variables defined protected in the class but there does not seem to be any specific value specified for any of those variables and there is no constructor in the class to the pass the value to.
I am seriously confused as to where the variable must have been passed from.
This may be something really basic but any help would be appreciated.
what are the possible ways of passing variables to the class

$this refers to the current object. according to PHP documentation
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).
here is some detailed explanation about it. which may help you to understand
What does the variable $this mean in PHP?

this is because myclass is getting the data from otherClass... like this:
<?php
class otherclass{
public $name="Mike";
}
class myclass extends otherclass {
function dosmthtomyclass() {
print_r($this);
}
}
$test=new myclass();
$test->dosmthtomyclass(); //prints "[name] => Mike"

You need to go through the manual and/or tutorials on OOP. Because this is the only way you'll understand OOP. Start with this: http://www.php.net/manual/en/language.oop5.basic.php
$this refers to the current instance of the object. Read about PHP+visibility to understand why private varianbles/methods aren't visisble to child classes (extended classes).
Good luck!

$this refers to the current object of the class. Execute the following code for more clarity:
<?php
class abc {
var $val = 3;
function show()
{
print_r($this);
}
}
$ob = new abc();
$ob->show();

Maybe some background on using classes in php can help you:
$this is used to refer to the hierarchy within the class.
E.g., you could have a class like this:
class Phpclass{
public function main(){
echo "public function called<br/>";
$this->helloworld();
}
private function helloworld(){
echo "hello world";
}
}
$phpclass=new Phpclass();
$phpclass->main();
This class is a blueprint of an object that will be instantiated with the variables $phpclass. As main() is a plublic function in the class, it can be called from outside the class. The private function can only be called from inside the class, so the main() function uses $this as identifier for the class itself to call the private function helloworld() inside itself. Without $this, the object wouldn't know that you are referring to a function inside itself.
First, the above will echo out "public function called", then "hello world".

Related

How to hide some fields through a trait in Laravel [duplicate]

I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.
How can I get this to work?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
This is the correct way
public static function userNameAvailibility()
{
$result = self::getsomthin();
}
Use self:: instead of $this-> for static methods.
See: PHP Static Methods Tutorial for more info :)
You can't use $this inside a static function, because static functions are independent of any instantiated object.
Try making the function not static.
Edit:
By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.
Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.
<?php
class some_class{
function nonStatic() {
//..... Some code ....
}
Static function isStatic(){
$someClassObject = new some_class;
$someClassObject->nonStatic();
}
}
?>
The accessor this refers to the current instance of the class. As static methods does not run off the instance, using this is barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.
It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class
In the static method,properties are for the class, not the object.
This is why access to static methods or features is possible without creating an object.
$this refers to an object made of a class, but $self only refers to the same class.
Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)
class A {
public function aMethod() {
echo "I'm A: ";
echo "printing " . $this->property;
}
}
class B {
public $property = "B property";
public function bMethod() {
A::aMethod();
}
}
$b = new B();
$b->bMethod();
It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.
EDIT:
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).
PHP > The Basics

Assigning values to undeclared properties

I don't understand a concept of class in php. I could be wrong.
I looked at a WordPress plugin. The class was defined and the properties of class wasn't created only functions were created.
Consider this example
class a
{
public function show(){
echo "hello";
$this->something = "xyz" ;
// What is this? How can "something" can be used here;
// as it is not defined in the class?
}
}
Then an object of that class was created in another file.
$obj = new a();
$obj->anothersomething = "abc"; // is it possible?
Enlighten me please.
My question is: Can we assign a value to undeclared property of a class?
Default class visibility is public.
However, it is good practice to explicitly declare class method with it's visibility.
class Foo
{
public function a() {}
protected function b() {}
private function c() {}
}
As #SougataBose mentioned, I'd suggest you running through PHP OOP course
Edit:
When it comes to properties - yes. It is possible to create them dynamically. Again, as a good practice, it is recommended to declare all properties in class body.
In this case it's not a function, but a public class method. Normally you need to define it with public/protected/private keyword, but when skipped, it's just public by default. So then in another file you create an instance of this class and call the public method show() which can then use class instance properties direct. Or you can assign these properties from outside using $obj->anothersomething = "xxx", which is not a good practice. All the assignments should be done through setter methods like this $obj->setProperty($value);

Call a private $var via a static method in php without instanciating any object [duplicate]

I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.
How can I get this to work?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
This is the correct way
public static function userNameAvailibility()
{
$result = self::getsomthin();
}
Use self:: instead of $this-> for static methods.
See: PHP Static Methods Tutorial for more info :)
You can't use $this inside a static function, because static functions are independent of any instantiated object.
Try making the function not static.
Edit:
By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.
Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.
<?php
class some_class{
function nonStatic() {
//..... Some code ....
}
Static function isStatic(){
$someClassObject = new some_class;
$someClassObject->nonStatic();
}
}
?>
The accessor this refers to the current instance of the class. As static methods does not run off the instance, using this is barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.
It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class
In the static method,properties are for the class, not the object.
This is why access to static methods or features is possible without creating an object.
$this refers to an object made of a class, but $self only refers to the same class.
Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)
class A {
public function aMethod() {
echo "I'm A: ";
echo "printing " . $this->property;
}
}
class B {
public $property = "B property";
public function bMethod() {
A::aMethod();
}
}
$b = new B();
$b->bMethod();
It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.
EDIT:
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).
PHP > The Basics

static variable not accessible via an object or $this but static functions are accessible via $this or object in PHP

<?php
class c1
{
public static function f1()
{
return "hello";
}
public static $a=10;
public function f2()
{
echo $this->f1(); //prints "hello"
echo $this->a;//ERROR:Undefined property: c1::$a in C:\wamp\www\class_in_php\example5.php on line 14
}
}
$obj1=new c1;
$obj1->f2();
?>
Why can't we access a static variable of a class using $this or an object of that class???
But we can access a static function of that class using $this or an object of that class.
What is the reason behind such a phenomenon?
You should use self:: instead of $this-> to access static members.
The reason is that $this refers to the current instance of the class, while static members are part of the class itself, not of the instance.
A static variable belongs not to an "instance" but to the class itself. When you have in actual "instance" of the class at runtime, then and only then does the $this pointer make sense: it means "this instance that I find myself inside right now"... how could you use the $this pointer to reference something that doesn't exist outside of an instance?
When I first learned C++ it was with (Metacomco I think) a system that actually used a huge pile of C preprocessor macros to simulate objects and it was very enlightening to see and hence understand that the $this (this in C++) is in fact just an extra parameter passed as the first parameter to all method functions:
this->foo("Hello");
this->bar(42, "Finished");
is actually executed like this:
foo(this_ptr, "Hello");
bar(this_ptr, 42, "Finished");
and inside the foo() function any reference to a method variable such as:
this->status
is nothing more than a reference to a pointer dereferenced variable:
this_ptr->status
So you can see that trying to access a static variable from a this pointer is going to blow because it just isn't a member of that particular chunk of memory. That's how things "used to work" but I think the explanation is still a good one.
Hope that help!
:)
Why can't we access a static variable of a class using $this or an object of that class? But we can access a static function of that class using $this or an object of that class.
Well, we can, however you used the wrong syntax.
Wrong:
echo $this->a;
Right:
$this::$a;
As c1::$a is a static class variable, you need to use the right syntax, that is with double-colon :: and then the dollar-sign ($) to denote the variable: $this::$a.
However, do not get fooled by that syntax too easy, because the reason that
$this->f1()
works while c1::f1() is a static function is because of backwards compatibility as before PHP version 5 there were no static class methods (as those explicitly defined by the static keyword) and with the very first PHP 5 version -> could be used to call static class methods.
However to access static class variables via $this is a PHP 5.3+ syntax feature, so much newer.
Example code (run against multiple PHP versions):
<?php
/**
* #link http://stackoverflow.com/a/24059368/367456
*/
class c1
{
public static function f1()
{
return "hello";
}
public static $a = 10;
public function f2()
{
echo $this->f1(); // prints "hello"
echo $this->a; // Strict Standards: Accessing static property c1::$a as non static
echo $this::$a; // prints "10" (PHP <= 5.2.17: Parse error)
}
}
$obj1 = new c1;
$obj1->f2();

Using $this inside a static function fails

I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.
How can I get this to work?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
This is the correct way
public static function userNameAvailibility()
{
$result = self::getsomthin();
}
Use self:: instead of $this-> for static methods.
See: PHP Static Methods Tutorial for more info :)
You can't use $this inside a static function, because static functions are independent of any instantiated object.
Try making the function not static.
Edit:
By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.
Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.
<?php
class some_class{
function nonStatic() {
//..... Some code ....
}
Static function isStatic(){
$someClassObject = new some_class;
$someClassObject->nonStatic();
}
}
?>
The accessor this refers to the current instance of the class. As static methods does not run off the instance, using this is barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.
It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class
In the static method,properties are for the class, not the object.
This is why access to static methods or features is possible without creating an object.
$this refers to an object made of a class, but $self only refers to the same class.
Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)
class A {
public function aMethod() {
echo "I'm A: ";
echo "printing " . $this->property;
}
}
class B {
public $property = "B property";
public function bMethod() {
A::aMethod();
}
}
$b = new B();
$b->bMethod();
It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.
EDIT:
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).
PHP > The Basics

Categories