1

So I have a class for curl myCurl and I would like to use it for my 2 other classes but in those classes I want myCurl to only have one instance / be static so all objects of those other classes will use the same curl object.

class mycurl { 
   ...
}

And the class that I want to have just one instance of MyCurl

class Company{
private static $curl=new Mycurl();
...
}

This doesn't work syntax error, unexpected 'new' (T_NEW)

Higeath
  • 541
  • 5
  • 20
  • *...I would like to use it for my 2 other classes but in those classes I want myCurl to only have one instance / be static so all objects of those other classes will use the same curl object.* Based on your requirement, it seems like you're looking for [Singleton pattern](http://stackoverflow.com/questions/203336/creating-the-singleton-design-pattern-in-php5). – Rajdeep Paul Jun 12 '16 at 21:01
  • You can only assign values, not objects, during design time. Runtime you can assign object to properties by initializing the class in a constructor or factory. The only other value it accepts within a class is a constant within the *same* class. – Raphioly-San Jun 12 '16 at 21:02
  • @RajdeepPaul Well I would want to have a separate object of curl in different class so basically 1 class 1 curl object. – Higeath Jun 12 '16 at 21:08
  • @Higeath I've given an answer below. Hopefully this will resolve your issue. – Rajdeep Paul Jun 12 '16 at 21:59

3 Answers3

3

You can't initialize class instances in class body in PHP.

To make your example work, you'd have to do something like this:

class Company {
    private static $curl;

    public function __construct() {
        if (null === static::$curl) {
            static::$curl = new Mycurl();
        }
    }
}

Or, maybe a bit nicer way would be:

class Company {
    private static $curl;

    private static function curl() {
        if (null === static::$curl) {
            static::$curl = new Mycurl();
        }
        return static::$curl;
    }
}

This way, it won't be initialized until you actually need it.

Igor Pantović
  • 9,107
  • 2
  • 30
  • 43
  • Would it make more sense to make curl class a singleton and change all of its properties constantly between classes or this way? – Higeath Jun 12 '16 at 21:02
  • I don't know if I'm reading it correctly, but here's my 50 cent... It's wise to create a new curl instance for each individual call. The call options of the previous call do not get reset once the call is done. So you could unintentionally send (wrong) data to the next endpoint... So actually making a 'static' curl object isn't that wise... – Raphioly-San Jun 12 '16 at 21:05
  • Well the main curl setting for this class would remain the same and before the call I only change in that curl CURLOPT_URL so it would always get the correct data. – Higeath Jun 12 '16 at 21:08
  • I meant other options will have the same value as your previous call (e.g. `CURLOPT_RETURNTRANSFER`). It may lead to undesirable behavior... – Raphioly-San Jun 12 '16 at 21:10
1

You should do it like this:

class Company{
    public function getMycurlInstance()
    {
        static $mycurl;

        if ($mycurl === null) {
            $mysql = new Mycurl();
        }

        return $mycurl;
    }
}
Raphioly-San
  • 403
  • 3
  • 10
  • well that would create new Mycurl for each object of Company which I'm trying to avoid. – Higeath Jun 12 '16 at 21:10
  • this will create a static var within the method which isn't shared with the rest of the world, but is it a bad thing per se to make a new object for each call as in my old example? At least you get a fresh start... And just think of how often it will be re-used in the very same way as the first call... That's a very small probability... – Raphioly-San Jun 12 '16 at 21:14
1

From your comment,

I would want to have a separate object of curl in different class so basically 1 class 1 curl object.

Suppose you have three classes: MyCurl, Class1 and Class2, and you want to use only one instance of MyCurl in Class1 class and one instance of MyCurl in Class2 class, the solution would be like this:

class MyCurl { 

    private static $curlInstances = array( 'Class1' => null, 'Class2' => null);

    private function __construct(){}

    public static function getInstance($class){
        if(in_array($class, array_keys(self::$curlInstances))){
            if(self::$curlInstances[$class] == null){
                self::$curlInstances[$class] = new MyCurl();
            }
            return self::$curlInstances[$class];
        }else{
            return false;
        }
    }

    // your code
}

class Class1{
    private static $curlInstance;

    public static function getCurlInstance() {
        if(!isset(self::$curlInstance)){
            self::$curlInstance = MyCurl::getInstance(get_class());
        }
        return self::$curlInstance;
    }

    // your code
}

class Class2{
    private static $curlInstance;

    public static function getCurlInstance() {
        if(!isset(self::$curlInstance)){
            self::$curlInstance = MyCurl::getInstance(get_class());
        }
        return self::$curlInstance;
    }

    // your code
}

The explanation is given below:

In MyCurl class:

  • Create a private static class array $curlInstances. This array will be used to check whether an object has been created for the particular class or not.
  • Make its constructor method private. It prevents objects from being created from being created from outside of the class.
  • Create a static class method getInstance(). It first checks whether this method has been called from either Class1 or Class2 class or not. If so then it checks whether an instance has been created for the particular class or not. If no object has been created then a new object will be created, otherwise the old object will be returned by the method.

Both Class1 and Class2 class are quite same. In these classes:

  • Create a private static class variable $curlInstance to hold the instance of MyCurl class.
  • Create a static class method getCurlInstance() to get the instance of MyCurl class using Singleton pattern.

To get unique MyCurl instances for these individual classes, do this:

$class1CurlInstance1 = Class1::getCurlInstance(); // returns only one instance of MyCurl class
$class2CurlInstance1 = Class2::getCurlInstance(); // returns only one instance of MyCurl class

And again, just for the sake of debugging, do this:

// Both $class1CurlInstance2 and $class2CurlInstance2 will have the same old unique MyCurl instances
$class1CurlInstance2 = Class1::getCurlInstance();
$class2CurlInstance2 = Class2::getCurlInstance();
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37