Jump to content

Recommended Posts

I just decided to start up on OOP, and it seems quite fun (yes, I find programming fun). But, I'm having trouble on this test OOP page.

 

This is suppose to return the private variable $name, and if it's empty, it's suppose to echo the no name to return message.

 

Code:

 

<?php

class findName
{
private $name;

public function setName($sname)
{
	if(!$sname)
	{
		$this->name = "There is no name to return.";
	}
	else
	{
		$this->name=$sname;
	}
}

public function getName()
{
	return $this->name;
}
}

$n = new findName();
$n->getName();

?>

 

If there is an easier way to do this, please let me now!

 

Link to comment
https://forums.phpfreaks.com/topic/232824-learning-oop/
Share on other sites

New code, still no output:

 

<?php

class findName
{
private $name;

public function setName($sname)
{
	if(!$sname)
	{
		$this->name = "There is no name to return.";
	}
	else
	{
		$this->name=$sname;
	}
}

public function getName()
{
	return $this->name;
}
}

$n = new findName();
$n->setName();
$n->getName();

?>

Link to comment
https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197571
Share on other sites

When I run your code, I get:

Warning: Missing argument 1 for findName::setName(), called in /home/xxxxxx/oop.php on line 26 and defined in /home/xxxxxx/oop.php on line 7

Plus you're not echoing the result.

Try:

<?php

class findName
{
        private $name;

        public function setName($sname='')
        {
                if(!$sname)
                {
                        $this->name = "There is no name to return.";
                }
                else
                {
                        $this->name=$sname;
                }
        }

        public function getName()
        {
                return $this->name;
        }
}

$n = new findName();
$n->setName();
echo $n->getName() . "<br>\n";
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197573
Share on other sites

When I run your code, I get:

Warning: Missing argument 1 for findName::setName(), called in /home/xxxxxx/oop.php on line 26 and defined in /home/xxxxxx/oop.php on line 7

Plus you're not echoing the result.

Try:

<?php

class findName
{
        private $name;

        public function setName($sname='')
        {
                if(!$sname)
                {
                        $this->name = "There is no name to return.";
                }
                else
                {
                        $this->name=$sname;
                }
        }

        public function getName()
        {
                return $this->name;
        }
}

$n = new findName();
$n->setName();
echo $n->getName() . "<br>\n";
?>

 

Ken

 

Thanks to both, much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/232824-learning-oop/#findComment-1197574
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.