PHP code will not echo out the attribute of my object - php

I am working on a lab for class and having a little trouble getting my echo statement to function properly. I am trying to store a sentence in a variable within an object and then retrieve and echo the value of that variable.
<?php
class MagicSentence {
public $sentence;
public function __construct($sentence) {
$this->setSentence($sentence);
}
public function getSentence() { return $this->$sentence; }
public function setSentence($sentence) {
$this->sentence = $sentence;
}
} // End class MagicSentence
$magicSentence = new MagicSentence("The cow jumped over the moon.");
?>
<html>
<head>
<meta charset="utf-8">
<title>Pete's Treats Candy Contest</title>
</head>
<body>
<?php
//include ('header.php');
echo 'The magic sentence is: ' . $magicSentence->getSentence();
?>
</body>
</html>

It should be:
public function getSentence() { return $this->sentence; }
Notice the missing $ on sentence. Just one of those things about PHP to remember.

Related

Having difficulty printing text from php function on html page

Here's an example of what I have going on:
common.php
<?php
class Common {
function test() {
echo 'asdf;
}
}?>
webpage.php
<?php
require_once("common.php");
?>
<html>
<body>
<?php test(); ?>
</body>
</html>
No matter what I've tried, I cannot get the function test to print any text to the page. With the actual webpage I was using, anything below the '' line didn't load with that portion included. I've been searching around for the past hour to figure this out, what am I doing wrong?
You have missing a ' closure also dont need a class for this, you should have just the function definition
<?php
function test() {
echo 'asdf';
}
?>
<?php
class Common {
function test() {
echo 'asdf'; // missing a ' closure added
}
}?>
You can access this function using a object of this class
<?php
require_once("common.php");
// instantiate the class before you use it.
$common = new Common(); // Common is a object of class Common
?>
<html>
<body>
<?php echo $common->test(); ?>
</body>
</html>
Alternatively, If you don't want to have a $common variable you can make the method static like this.
<?php
class Common {
static function test() {
echo 'asdf';
}
}?>
Then all you have to do to call the method is:
<html>
<body>
<?php echo Common::test(); ?>
</body>
</html>

PHP programing print constant value using scope resolution operator with class

I want to print constant value in PHP by scope resolution operator, but it is echo this : $p=new Ninja(); echo $p->show();
Please note that I have just started to learning so there could be mistakes....
<html>
<head>
<title>Scope it Out!</title>
</head>
<body>
<p>
<?php
class Person {
}
class Ninja extends Person
{
const stealt="MAXIMUM";
function show()
{
echo Ninja::stealth;
}
}
?>
$p=new Ninja();
echo $p->show();
</p>
</body>
</html>
Correct your spelling for stealth.
Ninja::stealth; should be self::stealth
NEVER put a class definition along with HTML. Create a separate class file and include it.
$p=new Ninja(); and echo $p->show(); are outside PHP tags.
class Person {
}
class Ninja extends Person
{
const stealt="MAXIMUM";
function show()
{
echo Ninja::stealt; // spelling mistake here..
}
}
//remove php closed tag from here...
$p=new Ninja();
echo $p->show();

PHP object _construct function

I have the following code
<!doctype html>
<head>
<meta charset = "utf-8">
<title>Objects</title>
</head>
<body>
<?php
class firstClass
{
function _construct($param)
{
echo "Constructor called with parameter $param";
}
}
$a = new firstClass('one');
?>
</body>
</html>
When i run this code nothing is outputted in the browser, the tutorial i am following says this code should output "Constructer called with parameter apples", what is the problem?
The constructor should be __construct() with two underscores.
http://php.net/manual/en/language.oop5.decon.php
And it will output "Constructor called with parameter one" in your code.
You have missed a '_' in the constructor definition.
function _construct($param) => defines a function called _construct
with one parameter function __construct($param) => defines custom
constructor with one parameter
The code should be like this:
<?php
class firstClass
{
function __construct($param)
{
echo "Constructor called with parameter $param";
}
}
$a = new firstClass('one');
?>

PHP singleton not accessible

i've got a bit of a problem with getting a singleton to work in php 5.3
What i want to achieve is that i'll be able to include one php file with a class,
that lets me translate webpages by a dictionary over a global singleton.
Usage example:
<?php
include_once "CLocale.php";
?>
//...
<head>
<title><?php CLocale::Instance()->getText("StrMemberArea")?></title>
My class looks like the following at the moment:
class CLocale
{
private function __clone()
{
}
public static function Instance()
{
if (static::$_instance === NULL)
{
static::$_instance = new static();
}
return static::$_instance;
}
private function __construct()
{
}
public function getText($str)
{
return "Test";
}
}
So, the problem is, i don't get any output of "Test" when using the class like shown above,
also, i don't get any error. PHP Storm isn't really showing me any errors.
Perhaps one of you guys is able to spot a problem somewhere.
Thanks in advance,
calav3ra
PS: I don't mind how the singleton is implemented
Yo forgot to echo the result
<title><?php CLocale::Instance()->getText("StrMemberArea")?></title>
should be:
<title><?php echo CLocale::Instance()->getText("StrMemberArea")?></title>
Ehm - the Singleton code is completely right, but you forgot to output the return value from getText
<?php
include_once "CLocale.php";
?>
//...
<head>
<title><?php echo CLocale::Instance()->getText("StrMemberArea")?></title>
To get anything displayed you no just need to return it, but also echo or print it. Like this:
<title><?php echo CLocale::Instance()->getText("StrMemberArea")?></title>

calling methods in object oriented php

I'm sure this will look like stupid question for most of you. However, I've been banging my head for quite a while over it.
Coming from ASP.NET/C#, I'm trying to use PHP now. But the whole OOrintation gives me hard time.
I have the following code:
<html>
<head>
</head>
<body>
<?php
echo "hello<br/>";
class clsA
{
function a_func()
{
echo "a_func() executed <br/>";
}
}
abstract class clsB
{
protected $A;
function clsB()
{
$A = new clsA();
echo "clsB constructor ended<br/>";
}
}
class clsC extends clsB
{
function try_this()
{
echo "entered try_this() function <br/>";
$this->A->a_func();
}
}
$c = new clsC();
$c->try_this();
echo "end successfuly<br/>";
?>
</body>
</html>
To my simple understanding this code should result with the following lines:
hello
clsB constructor ended
entered try_this() function
a_func() executed
however, it does not run 'a_func', all I get is:
hello
clsB constructor ended
entered try_this() function
Can anyone spot the problem?
Thanks in advanced.
Your problem lies here:
$A = new clsA();
Here, you're assigning a new clsA object to the local variable $A. What you meant to do was assign it to the property $A:
$this->A = new clsA();
As the first answer but also you could extend the b class to the a class this way you can access the a class in C, like below:
<?php
echo "hello<br/>";
class clsA{
function a_func(){
echo "a_func() executed <br/>";
}
}
abstract class clsB extends clsA{
function clsB(){
echo "clsB constructor ended<br/>";
}
}
class clsC extends clsB{
function try_this(){
echo "entered try_this() function <br/>";
self::a_func();
}
}
$c = new clsC();
$c->try_this();
echo "end successfuly<br/>";
?>

Categories