2

Is there a way to do something like this:

class Test {
    if(!empty($somevariable)) {
        public function somefunction() {

        }
    }
}

I know this might not be best practice, but I need to do this for a very specific problem I have, so is there anyway to do it?

I just want that function to be included in the class if that variable (which is tied to a URL param) is not empty. As it is written now, I get Error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION

Thanks!

3
  • 1
    I need to do this for a very specific problem I have - you are welcome to express your very specific problem here and get the proper solution. It would be way better than trying to get help in moving a square wheel you invented. To let you know, most of "very specific problems" turns out to be simple and trivial cases that have commonplace solutions. Commented Sep 5, 2013 at 14:48
  • Call that function when that required variable is not empty. OR add that IF condition in the function, and that function code will run when that required variable is not empty. Commented Sep 5, 2013 at 14:49
  • I will try posting the specific problem, probably a better idea. Commented Sep 5, 2013 at 14:55

4 Answers 4

2

It depends on the your specific use case, and I don't have enough info to give a specific answer, but I can think of one possible fix.

Extend the class, using an if statement. Put everything except the one function in AbstractTest.

<?php
abstract class AbstractTest 
{
    // Rest of your code in here
}

if (!empty($somevariable)) {
    class Test extends AbstractTest {
        public function somefunction() {

        }
    }
} else {
    class Test extends AbstractTest { }
}

Now, the class Test only has the method somefunction if $somevariable isn't empty. Otherwise it directly extends AbstractTest and doesn't add the new method.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked, but I ended up fixing the core problem anyway. Thanks for everyone's help.
1

Call the required function if the variable is not empty.

<?php
    class Test {
        public function myFunct() {
            //Function description
        }
    }
    $oTest = new Test();
    if(!empty($_GET['urlParam'])) {
        oTest->myFunc();
    }
?>

1 Comment

I tried this, but I only get the desired result when this method exists in the class or not, so it doesn't work.
0
class Test {
    public function somefunction() {

    }
}

is all you need actually.

Please note that a function inside a class is called 'method'.

3 Comments

Thanks for the lingo lesson (Not being sarcastic, I really need to leran it), however that solution doesn't work for me, because I need the function to exist or not in order to get my desired result.
@user2278120 - it shouldn't make a difference if the function exists or not. A function should only have an effect if it's being called somewhere. Otherwise, it's a block of code that does nothing.
@user2278120 Ever considered having the method as exampled here, and then from PHP if (empty($var)){ $Class->method(); } out of the class scope? so then you only call it when needed.
0

AFAIK you cannot have a condition out of the method in class scope (if that flows)

Class Test {
 if (empty($Var)){
    public function Test_Method (){

    }
  }

}

Will not work. Why not have it constantly exisisting but only call the method when it's needed?

Example:

Class Test { 
  public function Some_Method(){
    return 23094; // Return something for example purpose
  }

}

Then from your PHP:

$Var = ""; // set an empty string
$Class = new Test();

if (empty($Var)){
  echo $Class->Some_Method(); // Will output if $Var is empty 

}

Perhaps you trying to validate a string within OOP scope, then take this example:

 Class New_Test {
     public $Variable; // Set a public variable 
    public function Set(){
      $This->Variable = "This is not empty"; // When calling, $this->variable will not be empty
    }
    public function Fail_Safe(){
      return "something"; // return a string
    }
  }

Then out of Scope:

  $Class = new New_Test();
  if (empty($Class->Variable)){
     $Class->Fail_Safe(); 
   } // Call failsafe if the variable in OOP scope is empty

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.