PHP Calling a function within a static method of a class - php

class MainClass {
public static function myStaticMethod(){
return myFunction();
function myFunction(){
echo 'hello';
}
}
}
The above code when executed returns call to undefined function myFunction();
Please, any ideas on how to call the function within the method?
Thank you

Move the function deceleration to before you attempt to use it when defining functions within other functions...
class MainClass
{
public static function myStaticMethod()
{
function myFunction()
{
echo 'hello';
}
return myFunction();
}
}
MainClass::myStaticMethod(); // No error thrown
Note that repeat calls to MainClass::myStaticMethod will raise Cannot redeclare myFunction() unless you manage that.
Otherwise, move it outside of your class
function myFunction()
{
echo 'hello';
}
class MainClass
{
public static function myStaticMethod()
{
return myFunction();
}
}

Related

How to call the same class function in controller

My sample code.. i dont know how to call
class sample
{
public function actionExample()
{
code here
// To call php function
}
public function a()
{
echo "test":
}
}
class className {
function mone() {
// CODE
}
function mTwo() {
// calling mone in mtwo
$this->mone();
}
}
bu using $this
eg:-
$this->functionName();
$this acts as an object of the containing controller and using this you can access the variables and functions as well.
eg:-
public $myvar;
You can access it using
$this->myvar='value';
similarly for functions
public function firstfun()
{
//some code
}
you can call it using
$this->firstfun()
TRy
class sample
{
public function actionExample()
{
$this->a();
// To call php function
}
public function a()
{
echo "test":
}
}
Check this for more reference.
http://www.yiiframework.com/forum/index.php/topic/6471-call-another-controllers-action/

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

Within a class : Access to a function A from a function B

I have a problem which is probably not for most of you.
Sorry if it is obvious for you...
This is my code :
class Bat
{
public function test()
{
echo"ici";
exit();
}
public function test2()
{
$this->test();
}
}
In my controller:
bat::test2();
i have an error:
Exception information: Message: Method "test" does not exist and was
not trapped in __call()
Bat::test2 refers to a static function. So you have to declare it static.
class Bat
{
public static function test()
{
echo"ici";
exit();
}
// You can call me from outside using 'Bar::test2()'
public static function test2()
{
// Call the static function 'test' in our own class
// $this is not defined as we are not in an instance context, but in a class context
self::test();
}
}
Bat::test2();
Else, you need an instance of Bat and call the function on that instance:
$myBat = new Bat();
$myBat->test2();

PHP : call variable from another function in class

This is my class code:
class myClass
{
public function myFunc()
{
$myvar = 'Test str';
}
public function result()
{
echo myClass::myFunc()->$myvar;
}
}
and I use this:
$nCls = new myClass;
$nCls->result();
To show Test str form myFunc() but nothing shown. I think the problem is :
echo myClass::myFunc()->$myvar;
Thanks for any help.
You are mixing up quite a few concepts.
First, you have to create a new object of class myClass:
$nCls = new myClass();
Then, you can call the member function (method) on that class:
$nCls->result();
In result(), you just call the other method using $this:
public function result()
{
echo $this->myFunc();
}
Note though that this does nothing. The variable $myvar is local and not a class attribute. I advise you read up on object oriented programming, and object oriented PHP in particular.
class myClass {
public $myvar;
public function myFunc() {
$this->myvar = 'Test str';
return $this;
}
public function result() {
echo $this->myFunc()->myvar;
}
}
$nCls = new myClass;
$nCls->result();
You can do this but this is not a good practice.
The problem is that you declare $myvar only in the scope of method myFunc(). That means it is not visible outside that method. Declare it as a class member instead:
class myClass
{
private $myvar;
public function myFunc()
{
$this->myvar = 'Test str';
}
public function result()
{
echo myClass::myFunc()->$myvar;
}
}
the problem is the scope, you can't call a variable within another function, define a property for the class and set it from a function then retrieve the property with result():
class myClass
{
public $myvar;
public function myFunc()
{
$this->myvar = 'Test str';
}
public function result()
{
echo $this->myvar;
}
}
include "views.php";
class Controller extends views{
function index(){
$this->head();
$this->home();
}
function privacy(){
$this->head();
$this->privc();
}
function about(){
$this->head();
$this->abt();
}
function address(){
$this->head();
$this->add();
}
}
$obj=new Controller();
if(isset($_GET['fun'])){
$obj->$_GET['fun']();
}
else{
$obj->index();
}
This is views.php code
class views{
function head(){
include "views/header.php";
echo "<br>this is header";
}
function abt(){
include "views/about.php";
echo "<br>This is about us page";
}
function home(){
include "views/home.php";
echo "<br>This is Home page";
}
function privc(){
include "views/privacy.php";
echo "<br>This is privacy page";
}
function add(){
include "views/address.php";
echo "<br>This is address page";
}
}

automatically call all method inside the class in php

Is there a way to call all the methods from a class once the class is initialized? For example, lets say I have a class named todo and once I make an instance of a todo class, all the methods/functions inside it will be executed, without calling it in the constructor?
<?php
class todo
{
function a()
{
}
function b()
{
}
function c()
{
}
function d()
{
}
}
$todo = new todo();
?>
In here I created an instance of a class todo so that the methods a, b, c, d will be executed. Is this possible?
This outputs 'abc'.
class Testing
{
public function __construct()
{
$methods = get_class_methods($this);
forEach($methods as $method)
{
if($method != '__construct')
{
echo $this->{$method}();
}
}
}
public function a()
{
return 'a';
}
public function b()
{
return 'b';
}
public function c()
{
return 'c';
}
}
I think you can use iterator. All methods will be called in foreach PHP Iterator
Use a __construct() method (as you mentioned), which is called on object instantiation. Anything else would be unfamiliar and unexpected (to have random methods instantly executed not by the constructor).
Your class code looks like you're using PHP4, if that's the case, name your constructor the same as the class name.
Like this? I use this pattern to register meta data about classes sometimes.
<?php
class todo {
public static function init() {
self::a();
self::b();
self::c();
self::d();
}
function a()
{
}
function b()
{
}
function c()
{
}
function d()
{
}
}
todo::init();
There isn't any way that I can think of, short of putting it into the constructor as you suggest in your question:
<?php
class todo
{
public function __construct()
{
$this->a();
$this->b();
$this->c();
$this->d();
}
function a()
{
}
function b()
{
}
function c()
{
}
function d()
{
}
}
$todo = new todo();
?>
I copied and pasted below class from php.net...
I thought it will be usefull because methods are not called using objects, instead using get_class_methods():
class myclass {
function myclass()
{
return(truenter code heree);
}
function myfunc1()
{
return(true);
}
function myfunc2()
{
return(true);
}
}
$class_methods = get_class_methods('myclass');
foreach ($class_methods as $method_name) {
echo "$method_name\n";
}

Categories