dani33l_87 Posted November 10, 2012 Share Posted November 10, 2012 <?php class Person { private $name; function __construct($name) { $this->name = $name; } function setName($newName) { $this->name = $newName; } function getName() { return $this->name; } } $brian = new Person("Brian Mischook"); // create object $name = $brian->getName(); // get name variable from object echo "Brian's full name: " . $name . '<br>'; // Need to add the code here. It must check the returned name length and print at logic output according to the name length ?> By using functions like strlen(), stripos() and substr(), I need to add code to the script that checks the length of the name property and if it is more than 10 characters long, the name property must be set to only be the first name. The class must not be changed. Exemple: Brian Full Name: Brain Mischoock Braian First Name: Brain Some suggestions? Quote Link to comment Share on other sites More sharing options...
Andy123 Posted November 10, 2012 Share Posted November 10, 2012 You could do like this: if (strlen($name) > 10) { $exploded = explode(' ', $name); // Split name by spaces and put into array $name = $exploded[0]; // $name now only contains the first name } Quote Link to comment Share on other sites More sharing options...
MMDE Posted November 10, 2012 Share Posted November 10, 2012 (edited) public function nameLength(){ return strlen($this->name); } or, but not recommended in many situations: public function getLength($var){ return strlen($this->{$var}); } first case to use: $person = new Person('dani33l_87'); echo $person->nameLength(); In the latter case to use: $person = new Person('dani33l_87'); echo $person->getLength('name'); Edited November 10, 2012 by MMDE Quote Link to comment Share on other sites More sharing options...
us2rn4m2 Posted November 10, 2012 Share Posted November 10, 2012 <?php class Person { private $name; function __construct($name) { $this->name = $name; } function setName($newName) { $this->name = $newName; } function getName() { return $this->name; } } $p = new Person('PHPFreaks dani33l_87'); if(strlen($p->getName()) > 10) { if(str_word_count($p->getName()) > 1) { $pos = strpos($p->getName(), ' '); // if($pos) { $name = substr($p->getName(), 0, $pos); // } } else { $name = substr($p->getName(), 0, 10); } $p->setName($name); } echo $p->getName(); // Result: PHPFreaks Quote Link to comment Share on other sites More sharing options...
dani33l_87 Posted November 10, 2012 Author Share Posted November 10, 2012 Thank you very much for your help guys. Quote Link to comment Share on other sites More sharing options...
ignace Posted November 10, 2012 Share Posted November 10, 2012 (edited) The class must not be changed. I just want to add that this functionality should indeed be in the Person class not outside of it, otherwise you will just be copying code around your project whenever you need the person's first name. class Person { private $fullName; public function __construct($fullName) { $this->fullName = $fullName; } public function getFullName() { return $this->fullName; } public function getFirstName() { $words = str_word_count($this->getFullName(), 1); return $words[0]; } } Also just returning the first word could as easily be 'Dr.' which is not what was intended. Edited November 10, 2012 by ignace Quote Link to comment Share on other sites More sharing options...
MMDE Posted November 10, 2012 Share Posted November 10, 2012 Without changing the class: <?php class Person { private $name; function __construct($name) { $this->name = $name; } function setName($newName) { $this->name = $newName; } function getName() { return $this->name; } } class Person2 extends Person{ public function nameLength(){ return strlen($this->getName()); } } $person = new Person2('the name'); echo $person->nameLength(); ?> <?php class Person { private $name; function __construct($name) { $this->name = $name; } function setName($newName) { $this->name = $newName; } function getName() { return $this->name; } } $person = new Person2('the name'); echo strlen($this->getName()); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.