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)

4
  • ...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. Commented Jun 12, 2016 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. Commented Jun 12, 2016 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. Commented Jun 12, 2016 at 21:08
  • @Higeath I've given an answer below. Hopefully this will resolve your issue. Commented Jun 12, 2016 at 21:59

3 Answers 3

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.

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

4 Comments

Would it make more sense to make curl class a singleton and change all of its properties constantly between classes or this way?
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...
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.
I meant other options will have the same value as your previous call (e.g. CURLOPT_RETURNTRANSFER). It may lead to undesirable behavior...
1

You should do it like this:

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

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

        return $mycurl;
    }
}

2 Comments

well that would create new Mycurl for each object of Company which I'm trying to avoid.
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...
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();

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.