1

I'm trying to call some "global/static" variables($agent, $version) inside a class but I don't know how to insert them in the class itself . Basically I declare the class "$myclass = new myClass($agent, $version) but I don't know how to call these variables inside the class (e.g. use them in the getProducts function ). I know the questions sounds stupid but I just don't get it .

index.php :

$myclass = new myClass($agent, $version);

$agent = "firefox";
$version = "234";

$catalog = $myClass->$getProducts("http://amazon.com", "red");

myclass.class.php :

class myClass {
    function getXML ($agent, $version) {
        //do something 
        return $
    }

    function getProducts ($url, $color) {
        $product = $this->getXML($agent, $version);
        $catalog =  str_replace("asus", "", $product);
        return $catalog
    }

}
3
  • you should declare the class first class myClass. Then initialize $agent and $version. Only after that you can instantiate it new myClass($agent, $version). But you did the reverse! Commented Jan 19, 2013 at 19:02
  • Why do you want to use static variables? Commented Jan 19, 2013 at 19:03
  • Make a constructor for the class and declare/initialize the variables of the class: php.net/manual/en/language.oop5.decon.php Commented Jan 19, 2013 at 19:05

4 Answers 4

1

It doesn't sound like static variables are what you're looking for and are best avoided unless you know what you're doing (they're not really object oriented and not needed at all in PHP 5.3+). From the code that you've provided it looks like your expecting to pass the arguments into the object instantiation (new), so you should create a constructor for the class that accepts the arguments and assigns them to instance variables to be used in the method.

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

4 Comments

sounds great but how ? Btw I just want to get the job done not really do any state of the art project.
Are you doing an application or school project? If latter, shouldn't you do it by yourself, and learn? ;) Though either way, you should't outsource your work.
I do some work for myself meaning that instead to do it manually I'm trying to automate some tasks (php curl , scarping etc ). I don't understand why is so complicated to pass the arguments into the class.
Read the answer of Niko, or just pass them directly like this: $instance = new myClass("Firefox", "1234"); I see no point of using static class variables in this case. Before this, you naturally declare/initialize the variables inside the class.
1

You can declare them using the static keyword.

From http://php.net/manual/en/language.oop5.static.php :

class Foo
{
    public static $my_static = 'foo';

    public function staticValue() {
        return self::$my_static;
    }
}

print Foo::$my_static . "\n";

3 Comments

I don't need $my_static = 'foo'; . I need the variables that were declared on the new class (e.g. $myclass = new Foo($a, $b, $c). I need the $a, $b, $c inside the class itself. How can I do that ?
But you need them inside the class, if you are doing OOP. You need to initialize the variables always first!
ok so is it impossible to get a variable that is outside of the class ? I mean isn't possible to pass it to the class itself when I initiate it ?
0

I suppose that you do not mean "static" variables but "properties":

class myClass {
    // Declare the properties
    private $agent, $version;

    public function __construct($agent, $version) {
        // Copy the arguments to the class members
        $this->agent = $agent;
        $this->version = $version;
    }

    public function getProducts($url, $color) {
        $product = $this->getXML();
        // ...
    }

    private function getXML() {
        // Use the values stored in $this->agent etc.
        $agent = $this->agent;
        // ...
    }
}

Usage:

$agent = "firefox";
$version = "234";
$instance = new myClass($agent, $version);
$catalog = $instance->getProducts("http://amazon.com", "red");

1 Comment

I don't know why I understand your answer but didn't understand the PHP doc which provided a similar example
0

This is also wrong:

function getProducts ($url, $color){
  $product = $this->getXML($agent, $version);
  $catalog =  str_replace("asus", "", $product);
  return $catalog
}

You cant pass $agent and $version variables in the getXML method unless you dont either, construct them first, or pass them in your getProducts method.

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.