Php类唯一数据成员

I'm really new in PHP, our instructor just teaching us C++ OOP and I want to try it on PHP.

I'm creating objects with my class.

class TwitterUser {

    private $twittername;
    public function TwitterUser($a)
    {
        $this->twittername = $a;
        // echo $this->twittername;


    }

}



  $reader = new Spreadsheet_Excel_Reader($target_path);

$veriler = $reader->sheets[0]['cells'];
foreach($veriler as $veri)
{
if(!empty($veri[$sutun]) and $veri[$sutun]!="Twitter")
    {
    $kisiler[] = new TwitterUser(temizle($veri[$sutun]));

    }
}

What I want is, if one object has same string with other object in $twittername data member, don't create new object.

This task is usually done using some kind of Model -> database approach (such as Doctrine), in which case you save the model data into database. The database table should be designed to not allow the same name for more than one record and the logic to enforce and error handle this can be built into the model class.

You can achieve the same by pure PHP, but it requires existing instances to be stored somehow so when creating new instances, existing ones can be checked for uniqueness.

You don't want to add the object if the username is test? Basically you can't back out of a constructor. Just add a simple flag to only add "test" user once.

Using your code sample:

$testuserexists = false;
foreach($veriler as $veri)
{
    if(!empty($veri[$sutun]) and $veri[$sutun]!="Twitter" && $testuser == false)
    {
        $kisiler[] = new TwitterUser(temizle($veri[$sutun]));
        if ($veri[$sutun] == "Test")
            $testuserexists = true;

    }
}

Or if you are trying to not have duplicates:

foreach($veriler as $veri)
{
    if(!empty($veri[$sutun]) and $veri[$sutun]!="Twitter" && !isset($kisiler[$veri[$sutun]]))
    {
        $kisiler[$veri[$sutun]] = new TwitterUser(temizle($veri[$sutun]));
    }
}

I don't know what the temizle function is supposed to do, but basically you can assign the username as the associative array key and prevent duplicates by adding an isset() to your conditional.