PHP singleton not accessible - php

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>

Related

PHP Call a function from another function in same class

I am unable figure out how to make this work any help will be appreciated
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
?>
You were rightly advised to create an instance of your class then call the method on it but you said
see thats what i don't want .....i want some way to make it work without adding those two lines...by doing something else...just not that...and i can't figure out what i can do.
That something else is Simple! Make your method static
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.
public static function display()
{
$w ="its working";
self::show($w);
}
Then you can just do
some::display();
Fiddle
well it is working if you add the last two lines:
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
$x = new some;
$x->display();
?>
see here and click on "execute code"
Seems you are not called to display() function. Call to that function and try again.

Setting Up PHP Class's without a framework

Does anyone have a guide or can provide me with a quick example how to set up a class and pass the data from the class to the index.php?
I was thinking I could do it like I do in a framework can I not?
$this->class->function();
coolclass.php
class coolClass
{
public function getPrintables()
{
return "hello world!";
}
}
index.php
include 'coolclass.php';
$instance = new coolClass();
echo $instance->getPrintables();
A quick example:
Foo.php
class Foo {
public __construct() {
..initialize your variables here...
}
public function doSomething() {
..have your function do something...
}
}
Index.php:
Include('Foo.php');
$my_Foo = Foo();
$my_Foo->doSomething();
I think its fairly straight-forward as to what's happening here...so ill leave it at that. Don't want to give too much away.

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/>";
?>

how to use $this inside a function that is inside a method?

I have a function that is inside a class method.
In the method I can refer to $this but I cannot in the function. It will return this error:
Fatal error: Using $this when not in
object context in
/var/www/john/app/views/users/view.ctp
on line 78
Here is an example of what I mean:
class View
{
var $property = 5;
function container()
{
echo $this->property;
function inner()
{
echo "<br/>Hello<br/>";
echo $this->property;
}
inner();
}
}
$v = new View();
$v->container();
You can test it here pastie
Is there a work around to make this work?
I know I can pass $this in as a parameter, but is there any other way? Using global $this gives an error also.
If your curious why I need this, its because my method is a view in an MVC model (or so it seems - I am using Cake), and in the view I need to use a function, and in the function I need to refer to $this.
do not make a function within another function, try this:
class View {
var $property = 5;
function container() {
echo $this->property;
$this->inner();
}
function inner() {
echo "<br/>Hello<br/>";
echo $this->property;
}
}
why not pass it with a parameter?
function inner($instance)
{
echo "<br/>Hello<br/>";
echo $instance->property;
}
inner($this);
I cannot yet comment, so I'm posting this answer instead.
This question seems CakePHP related to me, and I think you're overdoing the View. As an example consider reading Post Views from the CakePHP Blog Example
To sum up: if you are inside a CakePHP View, then you just output HTML with embedded PHP. The View has access to variables that you have set in the Controller action (i.e. UserProfiles::index). What I'd suggest is using something like the following:
<h2>UserProfile for <?php echo $user->name; ?></h2>
<?php if( $user->isAdmin() ): ?>
<p>You're an admin</p>
<?php else: ?>
<p>You're just a user</p>
<?php endif; ?>
Also I'd suggest looking at Elements, you can include them conditionally if required conditions are met ;).
When PHP comes across a function definition, it defines the function into the global scope. So although you declare your function within a method, the scope of the function is actually global. Therefore the only way to access $this from within inner() would be to pass it as a parameter. And even then it would behave slightly differently than $this because you wouldn't be within the scope of the object.

Multilingual php class - shortcut function

Good evening,
In my app that I'm currently developing, I have a class that handles multilinguism. It does so by externally loading an associative array, where a translation source would be defined something like this:
'Source input' => 'Zdroj vstupního'
Currently this works flawlessly by addressing and using the class the following way:
$lang = new Lang('Czech');
print $lang->_('Source input'); // output: "zdroj vstupního"
However, I want to have this in a short cut function that does not depend on an instance of the "Lang" class. I've tried experimenting with static methods, but so far I'm out of luck.
Pseudo code of what I want to achieve.
$lang = new Lang('Czech');
$lang->setCurrent('contact_us'); // loads the language file for contact_us
<p>
<?php print _('Source input'); ?> // output: "zdroj vstupního"
</p>
A point in the right direction would be great. Thanks!
You can access the global $lang variable from your _ function if you use a global $lang statement:
<?php
class Lang
{
function _($str)
{
return 'Zdroj vstupního';
}
}
function _($str)
{
global $lang;
return $lang->_($str);
}
$lang = new Lang('Czech');
print _('Source input');
?>
Alternatively you could use a static variable in the Lang class instead of creating an instance of the class. It's a little cleaner this way as you avoid having to create a $lang variable in the global namespace.
<?php
class Lang
{
static $lang;
function setCurrent($lang)
{
self::$lang = $lang;
}
}
function _($str)
{
if (Lang::$lang == 'Czech')
{
return 'Zdroj vstupního';
}
}
Lang::setCurrent('Czech');
print _('Source input');
?>

Categories