php multiple classes and methods with same name - php

If I call "myClass::getItems()" from the "workingCLass" which getId method will be called? The one that echos "hello" or "bye"? Thank you.
class myClass extends otherClass {
function getId(){
echo "hello";
}
}
class otherClass{
function getItems(){
$this->getId();
}
function getId(){
echo "bye";
}
}
class workingClass extends myClass {
function __construct(){
$this->getItems();
}
}

The one with "hello", because you explicitly specified which one to call.
The problem, though, is that it's not static and you call it in a static context.
EDIT: With $this, it will not call anything, because there's no getItems() in the workingClass. If workingClass extended the otherClass it would do the "bye" thingie.

This will result in fatal error, since you are calling this method statically (::) and inside this method you are using $this special variable, that refers to workingClass(object from which it was called) and workingClass has no getId method.
OK, now after fixing the example I can say that it will output 'hello', because when calling method from the object ($this->) PHP will always run one defined in latest child class.

Related

How to call a method from a dynamically instantiated class in PHP?

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();

PHP 5.3 strange behaviour on static:: late binding

<?php
class baseModel
{
public static function show($className=__CLASS__)
{
print_r($className);
}
public function test()
{
static::show();
}
}
class AModel extends baseModel
{
public static function show($className=__CLASS__)
{
print_r($className);
}
}
class Controller
{
public function actionTest()
{
baseModel::test();
AModel::test();
}
}
$c = new Controller;
$c->actionTest();
?>
Expected output:
baseModelAModel
Actual output:
Fatal error: Call to undefined method Controller::show() in /var/www/dashboard/test.php on line 12
Why did PHP try to find Controller::show() rather than AModel::show?
static keyword refers to context, not the class the method was defined in. So, when you call A::test from context of C, static refers to C.
As it writes in php manual
In non-static contexts, the called class will be the class of the object instance. Since $this-> will try to call private methods from the same scope, using static:: may give different results. Another difference is that static:: can only refer to static properties.
This means that in context of object $c (class Controller) making a non-static call ($c->t()) late binding static:: keyword in method baseModel::test() references class of object reference $this which is Controller and not called class, while if calling static:
Controller::test();
Context of static:: is called class.
I would however advise you not to use static calls unless methods are explicitly defined as static:
class baseModel
{
public static function show($className=__CLASS__)
{
print_r($className);
}
public static function test()
// must be defined as static or context of static:: may change to Controller instead of actual class being called!
{
static::show();
}
}
This code works because if defining explicitly baseModel::test() as being static context of static:: will always be static.

Use $this-> when calling a class-scoped function (method)?

Is there a way to codify the following in PHP:
class myClass extends parentClass{
function myFunction(){
calculate();
}
}
class parentClass{
public function calculate(){
}
}
Or is $this-> always required?
class myClass extends parentClass{
function myFunction(){
$this->calculate();
}
}
You need $this-> in functions, defined as class methods.
Also you can use global functions out of classes, they neednot $this->
function myMethod() { echo "Outside Class Scope"; }
class A {
function myMethod() { echo "Inside Class Scope"; }
function what_to_call() {
myMethod();
}
}
What function should PHP execute when it encounters myMethod() within class A''s what_to_call() method?
Also consider a long chain of inheritance, with each ancestor having its own myMethod(). What method should PHP call? The current objects'? The parents'? The grandparents'?
You need to use this $this-> when you're calling method
$this-> must be used if you're referring to a method (i.e. a function defined inside of a class). calculate() on it's own refers to a function outside of the class (just like any build-in function), which is not what you want.
$this-> my_function
This statement is for call an function into a Class :)

call a static method inside a class?

how do i call a static method from another method inside the same class?
$this->staticMethod();
or
$this::staticMethod();
self::staticMethod();
More information about the Static keyword.
Let's assume this is your class:
class Test
{
private $baz = 1;
public function foo() { ... }
public function bar()
{
printf("baz = %d\n", $this->baz);
}
public static function staticMethod() { echo "static method\n"; }
}
From within the foo() method, let's look at the different options:
$this->staticMethod();
So that calls staticMethod() as an instance method, right? It does not. This is because the method is declared as public static the interpreter will call it as a static method, so it will work as expected. It could be argued that doing so makes it less obvious from the code that a static method call is taking place.
$this::staticMethod();
Since PHP 5.3 you can use $var::method() to mean <class-of-$var>::; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method:
self::staticMethod();
Now, before you start thinking that the :: is the static call operator, let me give you another example:
self::bar();
This will print baz = 1, which means that $this->bar() and self::bar() do exactly the same thing; that's because :: is just a scope resolution operator. It's there to make parent::, self:: and static:: work and give you access to static variables; how a method is called depends on its signature and how the caller was called.
To see all of this in action, see this 3v4l.org output.
This is a very late response, but adds some detail on the previous answers
When it comes to calling static methods in PHP from another static method on the same class, it is important to differentiate between self and the class name.
Take for instance this code:
class static_test_class {
public static function test() {
echo "Original class\n";
}
public static function run($use_self) {
if($use_self) {
self::test();
} else {
$class = get_called_class();
$class::test();
}
}
}
class extended_static_test_class extends static_test_class {
public static function test() {
echo "Extended class\n";
}
}
extended_static_test_class::run(true);
extended_static_test_class::run(false);
The output of this code is:
Original class
Extended class
This is because self refers to the class the code is in, rather than the class of the code it is being called from.
If you want to use a method defined on a class which inherits the original class, you need to use something like:
$class = get_called_class();
$class::function_name();
In the later PHP version self::staticMethod(); also will not work. It will throw the strict standard error.
In this case, we can create object of same class and call by object
here is the example
class Foo {
public function fun1() {
echo 'non-static';
}
public static function fun2() {
echo (new self)->fun1();
}
}
call a static method inside a class
className::staticFunctionName
example
ClassName::staticMethod();

php static function

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. :(

Categories