Jump to content

Class, Oop Php, Passing Parameters, Method Calls, String Functions


dani33l_87

Recommended Posts

<?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?

Link to comment
Share on other sites

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 by MMDE
Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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 by ignace
Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.