PHP object _construct function - php

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

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 code will not echo out the attribute of my object

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.

Using local variable from call in a function

After 9 hours of struggling to get this right, I have turned to the internet for help. I can't seem to find any relevant answers doing a Google search.
I currently have a class called Test. Test accepts a single argument.
<?php
class test {
private $varpassed;
public function getVarpas() {
return $this->varpassed;
}
Public function setVarpas($value) {
$this->varpassed= $value;
}
public function stringGen(){
$testvar = $this->varpassed;
echo $testvar;
}
}
The stringGen function should return the $varpassed variable whenever its called. The value for $varpassed is set using the setVarpas function. However, when ever I call the stringGen() method I only seem to be getting the following error:
Fatal error: Using $this when not in object context in file.php line 14.
Pointing to this line:
$testvar = $this->varpassed;
Is there any other way to pass the variable to the stringGen method? I've tried using:
self::$this->varpassed;
Which also throws an error.
first create an instance of the object (so you can use $this in the context), for example:
$test = new test();
then you can call:
$test->setVarpas('Hello World!');
now you can call:
$test->stringGen();
you have to do something like this
$var = new test();
$var->setVarpas("Hello");
$var->stringGen(); // this will echo Hello
$this is used when you are withing class. outside class you have to use class object.
1) Change this: class test() to class test
2) Create and instance first something like $t1 = new test();
3) Call the function $t1->setVarpas(5);
4) Now you can call the function $t1->stringGen();
Fixed:
<?php
class test
{
private $varpassed;
public function getVarpas() {
return $this->varpassed;
}
Public function setVarpas($value) {
$this->varpassed= $value;
}
public function stringGen(){
$testvar = $this->varpassed;
echo $testvar;
}
}
$t1 = new test();
$t1->setVarpas(5);
$t1->stringGen();
OUTPUT:
5
You should not declare a class with parentheses.
Use
class test {
instead of
class test(){

Reverse engineering on a php function

I know it isn't actually the reverse engineering
I have this function:
<?=$this->get('translator')->trans('dashboard.actions', array(), 'front');?>
I want to understand how to insert a function (using include maybe) that gets called when using that code.
I know the function name needs to be trans, and it has 3 arguments, but how to define it? Let me put this in a more easy way: In a php script how do I print Hello world when using $this->get('translator')->trans('dashboard.actions', array(), 'front');
It's just a regular class method:
class Traslator{
public function trans($a, $b, $c){
return 'Hello world';
}
}
Now, you only need another class method called get() that returns an instance of the previous class:
class Foo{
public function get(){
return new Traslator;
}
}
Full code:
<?php
class Traslator{
public function trans($a, $b, $c){
return 'Hello world';
}
}
class Foo{
public function get(){
return new Traslator;
}
public function test(){
?>
<?=$this->get('translator')->trans('dashboard.actions', array(), 'front');?>
<?php
}
}
$f = new Foo;
$f->test();
$this references the current object instance, and that variable may not be re-assigned. Meaning, to trick PHP into doing what you want requires to add code before and after the line you mention.
By creating a single class, the result can be obtained.
Before the <?=... line
<?php
class C {
function get($s) {
return $this;
}
function trans($s, $a, $f) {
return 'Hello world';
}
function hw() {
?>
Then the line
<?=$this->get('translator')->trans('dashboard.actions', array(), 'front');?>
Then need, to end the class, and call the method that does what you want
<?php
}}
$i = new C();
$i->hw();
Put the 3 blocks of code into a file and call PHP on it, the <?=... line should show "Hello world".
$this->get('translator') return instance of class lets call it "Translator" - on that instance you call trans() method
so you would like to change instance of "Translator" to your extended version
class ExtTranslator extends Translator{
public function trans($params){
echo 'fdsfds'; // here you could add what you like
return parent::trans($params);
}
}
and yes - it's hard to help you cause I don't really know what are you trying to do - to work with that code you must first understand it...

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>

Categories