开发者

PHP Call Function in a Class - Is there a simpler way?

开发者 https://www.devze.com 2023-04-11 03:34 出处:网络
I am very new to OOP and very rusty on PHP. I was wondering if this is a valid method to call a function from a class?

I am very new to OOP and very rusty on PHP. I was wondering if this is a valid method to call a function from a class?

class newclass {
    public function testfunc {
        return '1';
    }
}

Could I call it like this:

echo testfunc->newclass();

or like this:

echo new开发者_如何学Goclass()::testfunc;

I always see it defined in examples like below and it seemed like extra code?:

$this = new newclass();
$this->testfunc();
echo $this;

Any help would be greatly appreciated as I'm just starting to wrap my head around this OOP thing. If I'm out to lunch maybe someone could suggest a link to a really good guide for a true beginner in classes/OOP. Thanks!


Both ways work and have their use cases.

Your first case is a regular function call using an instance of a class, your second case is a call to a static function.

Static should be used with care and the use of it is very often a sign that refactoring/redesign is necessary.

The point of object oriented programming is to model the world by writing classes (blueprints) and then create as many independent instances of that class with the word new as needed. Each instance is a little organism with the DNA of the class and you can call the same class method on every single instance without influencing the other instances.

A static call however is not related to an instance of a class and therefore there is no object being used. It's a global call of some tool functionality and in fact breaks the idea of encapsulation.

So, I'm not saying there are no use cases for static classes and methods but they should be used with care.


new is the keyword to instantiate the class. If you want to use a method without an instance of the class, it should be a static method. to have a static method, declare the method as static.

class foo
{
    public static function bar()
    {
        return 'hello!'; 
    }
}

How to use it?

echo foo::bar(); //Will print hello


You could make testfunc static and call it like so:

class newclass{

  public static function testfunc{

     return '1';
  }
}

echo newclass::testfunc();

There is nothing like this echo testfunc->newclass(); and doing it like

$class = new newclass();
echo $class->testfunc();

is the proper way to do it when the method is an instance method and not a static one. Note, there is no ability to reference $this within the static method.


You can create a static wrapper for the constructor which would allow for chaining method calls on the same line.

<?php

class ClassA {

     static function create() { return new self(); }

     function method1() { ... }

}


ClassA::create()->method1();


you can include the php file that contains your functions

<?php
//print.php
function printHello(){
echo "Hello world";
}
?>

then include it and call the function...

<?php
include "print.php";
printHello();

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号