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();
}
}
I have 2 file
file1.php
<?php
Class A
{
public static function _test
{
}
}
function get_sql($id)
{
}
function get_data($ids)
{
}
?>
In file2.php I've written
require_once('file1.php');
$a = get_sql($id);
Why I cannot call the function and get my result??
try this in file1.php
<?php
Class A {
public static function _test {
}
function get_sql($id) {
echo $id;
}
function get_data($ids) {
}
}
?>
In file2.php first require the file and then code this
require_once('file1.php');
$a = new A();
$a->get_sql($id);
OR send static value in function
$a->get_sql(5);
This is your first mistake in your code
public static function _test{
}
} //this bracket is related to the class
Well for one thing you are not returning anything from the get_sql($id) function.
Assuming you are returning something in your original code; I hope you are aware that the function is not part of the class (its defined outside the scope of the class). But for educational purposes you would call a static method within a class by doing:
$a = A::get_sql($id);
This would also mean the defining the function in the following manner:
Class A{
public static function get_sql($id){
echo $id;
}
}
it is the question if you want to have functions get_sql() and get_data() as a methods inside the A class:
If yes the code from user2727841 will work after you add the round brackets to the function public static function _test:
public static function _test()
{
}
Your code will work too after you add the same brackets to the same function but your function get_sql() and get_data() are outside the class A.
EDIT
I thought that these function are outside the class A.
Please, add the round brackets to the public static function _test in the class A - it is syntax error - than I hope it will work.
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();
Can not call many function: Zend_View_Helper
helpers : MainHelpers.php
Class Zend_View_Helper_MainHelpers {
public function mainHelpers(){
$output="ok 1";
return $output;
}
public function mainHelpers2(){
$output="ok 2";
return $output;
}
}
view : detail.phtml
<?php echo $this->mainHelpers(); ?> // ok call function
<?php echo $this->mainHelpers2(); ?> // not ok call function
I want to call many function in zend_view_helper.
If you want your view helper to contain additional methods besides its constructor, make sure you return the object instance and do something like this:
Class Zend_View_Helper_MainHelpers {
public function mainhelpers() {
return $this;
}
public function foo(){
$output="ok 1";
return $output;
}
public function bar(){
$output="ok 2";
return $output;
}
}
Now call your helper methods like this:
$this->mainhelpers()->foo()
$this->mainhelpers()->bar()
Judging from your code example, it seems you are trying to encapsulate more than one view helper inside one class. You are probably better of by creating more view helpers:
class Zend_View_Helper_Foo()
{
public function foo()
{
// do stuff
}
}
class Zend_View_Helper_Bar()
{
public function bar()
{
// do more stuff
}
}
I have been trying to figure out how to go about doing this but I am not quite sure how.
Here is an example of what I am trying to do:
class test {
public newTest(){
function bigTest(){
//Big Test Here
}
function smallTest(){
//Small Test Here
}
}
public scoreTest(){
//Scoring code here;
}
}
Here is the part I am having problems with, how do I call bigTest()?
Try this one:
class test {
public function newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public function scoreTest(){
//Scoring code here;
}
}
$testObject = new test();
$testObject->newTest();
$testObject->scoreTest();
The sample you provided is not valid PHP and has a few issues:
public scoreTest() {
...
}
is not a proper function declaration -- you need to declare functions with the 'function' keyword.
The syntax should rather be:
public function scoreTest() {
...
}
Second, wrapping the bigTest() and smallTest() functions in public function() {} does not make them private — you should use the private keyword on both of these individually:
class test () {
public function newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public function scoreTest(){
//Scoring code here;
}
}
Also, it is convention to capitalize class names in class declarations ('Test').
Hope that helps.
class test {
public newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public scoreTest(){
//Scoring code here;
}
}
I think you are searching for something like this one.
class test {
private $str = NULL;
public function newTest(){
$this->str .= 'function "newTest" called, ';
return $this;
}
public function bigTest(){
return $this->str . ' function "bigTest" called,';
}
public function smallTest(){
return $this->str . ' function "smallTest" called,';
}
public function scoreTest(){
return $this->str . ' function "scoreTest" called,';
}
}
$test = new test;
echo $test->newTest()->bigTest();
To call any method of an object instantiated from a class (with statement new), you need to "point" to it. From the outside you just use the resource created by the new statement.
Inside any object PHP created by new, saves the same resource into the $this variable.
So, inside a class you MUST point to the method by $this.
In your class, to call smallTest from inside the class, you must tell PHP which of all the objects created by the new statement you want to execute, just write:
$this->smallTest();
In order to have a "function within a function", if I understand what you're asking, you need PHP 5.3, where you can take advantage of the new Closure feature.
So you could have:
public function newTest() {
$bigTest = function() {
//Big Test Here
}
}
class sampleClass
{
public function f1()
{
return "f1 run";
}
public function f2()
{
echo ("f2 run" );
$result = $this->f1();
echo ($result);
}
f2();
}
output :
f2 run
f1 run
You need to call newTest to make the functions declared inside that method “visible” (see Functions within functions). But that are then just normal functions and no methods.
example 1
class TestClass{
public function __call($name,$arg){
call_user_func($name,$arg);
}
}
class test {
public function newTest(){
function bigTest(){
echo 'Big Test Here';
}
function smallTest(){
echo 'Small Test Here';
}
$obj=new TestClass;
return $obj;
}
}
$rentry=new test;
$rentry->newTest()->bigTest();
example2
class test {
public function newTest($method_name){
function bigTest(){
echo 'Big Test Here';
}
function smallTest(){
echo 'Small Test Here';
}
if(function_exists( $method_name)){
call_user_func($method_name);
}
else{
echo 'method not exists';
}
}
}
$obj=new test;
$obj->newTest('bigTest')
You can also use self::CONST instead of $this->CONST if you want to call a static variable or function of the current class.