Jump to content

Learning OOP


3raser

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.