php class methods used statically without class reference? - php

I have a class like this:
class Utils{
public function hello()
{
return "<b>Hello World</b></br>";
}
}
Can I now do something directly like this in my page.php:
require_once("classes/Utils.php");
echo hello();
Or must I do this:
$u = new Utils;
$u->hello();
even though the function contains no object properties that need to be instantiated?

Yes, if you declare your function as static.
class Utils{
public static function hello()
{
return "<b>Hello World</b></br>";
}
}
and call it as
Utils::hello()
php static function

actually you call static methods like this:
Utils::hello();

You can call it directly like:
echo Utils::hello();
Here is a PHPFiddle example

Related

PHP constructor not called

I have problem, can i call constructor without create 'new class()' ? Or you maybe have another way for this :
<?php
class a
{
public static $hello;
public function say()
{
return self::$hello;
}
}
class b extends a
{
public function __construct()
{
self::$hello = 'hello world';
}
}
echo b::say();
?>
I have try with :
$b = new b();
echo $b->say();
And it's work. But i want to use b::say();
Can help me?
Thank you!!
Check out this. Is this good for you?
<?php
class a {
public static $hello;
public static function say() {
return self::$hello;
}
}
class b extends a {
public function __construct() {
self::$hello = 'hello world';
}
public static function factory() {
return new b();
}
}
echo b::factory()->say();
?>
Actually I couldn't find a way to do this without calling constructor. This is how the workaround looks like. factory is just a name. you can rename it.
calling class method (with constructors) without object instantiation in php
You have asked: "can i call constructor without create 'new class()' ?"The answer: No.
... Classes which have a constructor method call this method on each
newly-created object
You have requested "But i want to use b::say();"
b::say(); - is call of static method.You can't override non-static parent method to static. But you can restructure your base class class a to make say() method static.
<?php
class a
{
public static $hello;
public static function say()
{
return self::$hello;
}
}
class b extends a
{
public function __construct()
{
self::$hello = 'hello world';
}
}
The thing that you were missing was you needed to add your content to your new class method. - Just call it like so:
$b = new b('Some words');
echo $b->say();
When calling a new class and using a constructor - You will want to add the content in the paramaters for the new class you are making.
It acts as if you are calling the __construct function. - Calling new class($a) will call the __construct($a) function once making the object.
Hope that this helped a bit :)
Yes it is possible just make the say() function static like this :
public static function say()
{
return self::$hello;
}
Declaring class methods as static makes them accessible without needing an instantiation of the class.
This example looks to me like late static binding. So try changing that return self::$hello; into return static::$hello;

Calling a Wordpress function out of class

I have a woocommerce plugin that has a class Foo:
function wc_foo_init(){
class WC_Foo extends WC_Shipping_Method{
$var=get_option(); //gets an option for this session
function sayHello(){
echo $var;
}
new WC_Foo();
}
I want to call sayHello() out of the Foo class:
function bar(){
WC_Foo->sayHello();
}
But I get this error:
Fatal error: Call to a member function `sayHello` on a non-object.
You must instantiate class before make call of its methods:
$foo = new WC_Foo();
$foo->sayHello();
or if your php version is greater than 5.4 you can do:
(new WC_Foo())->sayHello();
Use $this selector if you are calling the method within the class
function bar(){
$this->sayHello();
}
If you want to call the method from other place,
you need to instantiate the class like this:
$object = new WC_Foo();
$object->sayHello();
Or make the method static like this:
public static function sayHello(){
echo "Hello";
}
And call it like this:
WC_Foo::sayHello();
// this way you dont need $object = new WC_Foo();
This might not be the full code, so it's pretty weird what you have there, but let's go.
Since you have a function to init your object, you would probably want to at least return that instance. This code might help you understand:
function init_foo(){
class foo{
function say(){
echo 'hello';
}
}
$foo1 = new Foo();
return $foo1;
}
function bar($foo3){
$foo3->say();
}
$foo2 = init_foo();
bar($foo2);
So, first we create the object and return it. Then we inject it in the bar function, just needing to call the method after that. (I used different var names so it's easier to understand scope)

Using class inside a function

I'm trying to use my class inside a function called inside()
I have something like:
class MyClass{
public function test(){ echo "test ok"; }
}
$mc = new MyClass();
function inside(){
global $mc;
$mc->test();
}
But this doesn't work :(
I get:
Fatal error: Call to a member function test() on a non-object in ...
One solution would be to pass the class $mc as an argument to the function inside() but I want something else :/
Any idea ?
Ty guys
Since the inside function depends on the MyClass class, it should be passed in as a dependecy injection.
$mc = new MyClass;
function inside(MyClass $mc){
$mc->test();
}
It looks like you need a refresher of the (Gang of Fours) injection design pattern, http://martinfowler.com/articles/injection.html
If all you want is to access a function residing inside a class you can define it as static
class MyClass {
static function test() {
echo "test ok";
}
}
Then you don't have to use a global:
function inside() {
MyClass::test();
}
You need to define test as public function test(){....

Use Class property inside of a method's function

I'm trying to use myVar inside my of a method's function. I have already tried adding global but still nothing. I know this is probably basic but I can't seem to find it.
class myClass{
public $myVar;
public function myFunction() {
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
}
}
Whenever I try using $this I get this error: 'Using $this when not in object context in...'
You should use $this->myVar
See the PHP Documentation - The Basics
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
?>
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
Update:
In your new code sample, myInnerFunction is a nested function and is not accessible until the myFunction method is called. Once the myFunction method is called, the myInnerFunction becomes part of the global scope.
Maybe this is what you are looking for:
class myClass{
public $myVar;
public function myFunction() {
}
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
}
Inner functions like myInnerFunction are always global in scope, even if they are defined inside of a member function in a class. See this question for another similar example
So, to PHP, the following are (almost) equivalent:
class myClass{
public $myVar;
public function myFunction() {
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
}
}
And
class myClass{
public $myVar;
public function myFunction() {
}
}
function myInnerFunction() {
//how do I use this variable here
echo $this->myVar;
}
Hopefully the second example illustrates why $this is not even in scope for myInnerFunction. The solution is simply to pass the variable as a parameter to the function.
Pass it as an argument to the inner function.
You can use ReflectionProperty:
$prop = new ReflectionProperty("SimpleClass", 'var');
Full example:
class myClass{
public $myVar;
public function myFunction() {
function myInnerFunction() {
//how do I use this variable here
$prop = new ReflectionProperty("SimpleClass", 'myVar');
}
}
}
The solution above is good when you need each instance to have an own value. If you need all instances to have a same you can use static:
class myClass
{
public static $myVar = "this is my var's value";
public function myClass() {
echo self::$myVar;
}
}
new myClass();
see here

What is meant by static invocation in definition of call_user_func

I have problems to understand the PHP manual for call_user_func, especially the parameter description:
The function to be called. Class methods may also be invoked statically using this function by passing array($classname, $methodname) to this parameter.
Example: Using a class method
<?php
class myclass {
function say_hello()
{
echo "Hello!\n";
}
}
$classname = "myclass";
call_user_func(array($classname, 'say_hello'));
?>
Also kindly explain this code line "call_user_func(array($classname, 'say_hello'))". Of course array($classname, 'say_hello') is not a method name.
Passing an array to call_user_func is a special case for invoking class methods, static and non-static. In the example you gave, you could do this:
<?php
class myclass {
public function say_hello()
{
echo "Hello!\n";
}
public static function say_hello_static() {
echo "Hello static!\n";
}
}
//Call static method
call_user_func(array('myclass','say_hello_static'));
//Call object method
$myobject = new myclass();
call_user_func(array($myobject,'say_hello'));
?>
As of PHP 5.2.3 you can call static methods by using a string, instead of an array, e.g:
call_user_func('myclass::say_hello_static');

Categories