I recently made a class in PHP
I am trying to declare a variable within class and using str_replace in a function but its show undefined variable
class Status{
$words = array(".com",".net",".co.uk",".tk","co.cc");
$replace = " ";
function getRoomName($roomlink)
{
echo str_replace($words,$replace,$roomlink);
}
}
$status = new Status;
echo $status->getRoomName("http://darsekarbala.com/azadari/");
Any kind of help would be appreciated thanks you
Your variables in the function getRoomname() aren't adressed properly. Your syntax assumes the variables are either declared within the function or passed while calling the function (which they aren't).
To do this within a class, do it while using $this->, like this:
function getRoomName($roomlink)
{
echo str_replace($this->words,$this->replace,$roomlink);
}
For further informations, please have a look into this page of the manual.
Maybe because of the version or something, when I tested your exact code, I got syntax error, unexpected '$words' (T_VARIABLE), expecting function (T_FUNCTION), so setting your variables to private or public should fix this one.
About the undefined varible, you have to use $this-> to access them from your class. Take a look:
class Status{
private $words = array(".com",".net",".co.uk",".tk","co.cc"); // changed
private $replace = " "; // changed
function getRoomName($roomlink){
echo str_replace($this->words, $this->replace, $roomlink); // changed
}
}
$status = new Status;
echo $status->getRoomName("http://darsekarbala.com/azadari/");
Also, since getRoomName isn't returning anything, echoing it doesn't do much. You could just:$status->getRoomName("http://darsekarbala.com/azadari/");.
or change to :
return str_replace($this->words, $this->replace, $roomlink);
Related
I am having some issues with my function which returns an array, I decided to try and use an OO approach to my php code and try to make a class with a few static functions since I decided I don't want to access it using an object. In my code, within the same class, I decided to make the following function:
public static function decideCategory($tweets) {
$tweet = $tweets;
if(in_array($tweet, self::$food)) {
echo "\nOur " . $tweet . " is under food\n";
} //if statements of the same nature below as well.
}
Now, this function works in the sense that it does not throw an error where $food is definded as an array at the top. However, originally I simply had $food defined at the top as just a private static variable, and then I had the following function which I passed into the in_array.
public static function getFood()
{
self::$food = array("Wendys", "McDonalds", "Wendy's", "Chic Fil A", "Chic-Fil-a", "Burger", "TGI", "BBQ", "Grilling", "Wine", "Tasty", "Yum", "IHOP", "Pancakes", "Pizza", "Cake"
,"Baking");
return self::$food;
}
However, it would return an error saying that in_array expects an array value for its second argument, but that instead it sees that a null was passed instead. Why is that and how can I use methods to do my comparison rather than the variables themselvs. If this were Java this would be how I would do it, and as such I cannot see why php would have these issues as it appears to follow a similar logic with returns.
Yes it would error because until you call self::getFood() Self::$food is null if you have declared it as
static $food;
update your method as below
public static function decideCategory($tweets)
{
$tweet = $tweets;
$food = self::getFood();
if(in_array($tweet, $food)) {
echo "\nOur " . $tweet . " is under food\n";
} //if statements of the same nature below as well.
}
I've got a simple question about variables in PHP!
How can i make a global final variable? I wanna have few information in a php-file. I've got some functions there who return the information.
<?php
static $titleOfPage = "Opinion";
static $autor = "Daniel";
static $description = "blablabla";
static $keywords = "blablabla";
function getTitleOfPage(){
return $titleOfPage;
}
function getAutorOfPage(){
return $autor;
}
function getDescriptionOfPage(){
return $description;
}
function getKeywordsOfPage(){
return $keywords;
}
?>
I include this file in another php file. And there i just wanna get the information from here. So when I do
echo getDescriptionOfPage();
I get the Error= Undefined variable: titleOfPage in /Applicat....
I only know the rules from C and JAVA..
Can anyone help me?
Thank you very much!
How can i make a global final variable?
So.. basically you want to define a named constant.
define("TITLE_OF_PAGE", "Opinion");
function getTitleOfPage(){
return TITLE_OF_PAGE;
}
But I'm pretty sure that there are no real reasons to do it. :P
Short Version: Why can't we access a function like this:
$b = "simple_print()";
$obj->$b;
Complete Version:
Suppose we have a class User defined like this:
class User {
public $name;
function simple_print() {
echo "Just Printing" . "<br>";
}
}
Now if a create an User object and set the name of it we can print its name using
$obj = new User;
$obj->name = "John";
echo $obj->name;
Although it is strange we also can do something like this in order to print "John":
$a = "name";
echo $obj->$a;
But we can't access a function using the same idea:
$b = "simple_print()";
$obj->$b;
Why? Shouldn't it work the same way?
Also, does anyone know what is it called? I tried to look for "accessing a member through a variable" and "using a method through a variable with the name of it" but I didn't find anything related to this.
Extra info: The version of PHP I'm using is: PHP version: 5.5.9-1ubuntu4.7
You were very close, but made a small logical mistake. Try this instead:
$b = 'simple_print';
$obj->$b();
This is because the method is accessed by it's name, which is simple_print, not simple_print(). The execution is triggered by the parenthesis, but that is not part of the name, so of how you access the method.
Here is a short example:
<?php
class Test
{
public function simple_print() {
echo "Hello world!\n";
}
}
$object = new Test;
$method = 'simple_print';
$object->$method();
As expected it creates the output Hello world! if executed on CLI.
Why is PHP Saying that my object is empty ?
Here is a simple use case.
It keeps returning the following error :
Fatal error: Cannot access empty property in C:\AdvancedApp\myApp.php on line 10.
class myClass
{
public $myFunction = "Hello World";
}
$class = new myClass();
echo $class->$myFunction;
The correct use of property is:
echo $class->myFunction;
What you did is used variable variables, the following will work:
$name = "myFunction" ;
echo $class->$name ;
Drop the $ sign from $myClass->$myFunction so it will be $myClass->myFunction, and what's with the variable name. Use something else, like myValue...
I am trying to assign a variable to a class in PHP, however I am not getting any results?
Can anyone offer any assistance? The code is provided below. I am trying to echo the URL as shown below, by first assigning it to a class variable.
class PageClass {
var $absolute_path = NULL;
function get_absolute_path(){
$url = $this->absolute_path;
echo $url;
}
}
$page = new PageClass();
$page->absolute_path = "http://localhost:8888/smile2/organic/";
$page->get_absolute_path(); //this should echo the URL as defined above - but does not
It also works for me.
Take a look at a live example of your code here.
However, there are a few things you should change about your class.
First, Garvey does make a good point that you should not be using var. That's the older PHP4, less OOP conscious version. Rather declare each variable public or private. In fact, you should declare each function public or private too.
Generally, most classes have private variables, since you usually only want to change the variables in specific ways. To achieve this control you usually set several public methods to allow client functions to interact with your class only in restricted predetermined ways.
If you have a getter, you'd probably want a setter, since these are usually used with private variables, like I described above.
A final note is that functions named get usually return a value. If you want to display a value, it is customary to use a name like display_path or show_path:
<?php
class PageClass
{
private $absolute_path = NULL;
public function set_absolute_path($path)
{
$this->absolute_path = $path;
}
public function display_absolute_path()
{
echo $this->absolute_path;
}
}
$page = new PageClass();
$page->set_absolute_path("http://localhost:8888/smile2/organic/");
$page->display_absolute_path();
// The above outputs: http://localhost:8888/smile2/organic/
// Your variable is now safe from meddling.
// This:
// echo $this->absolute_path;
// Will not work. It will create an error like:
// Fatal error: Cannot access private property PageClass::$absolute_path on ...
?>
Live Example Here
There's a section on classes and objects in the online PHP reference.
class PageClass {
public $absolute_path = NULL;
function get_absolute_path(){
$url = $this->absolute_path;
return $url;
}
}
$page = new PageClass();
$page->absolute_path = "http://localhost:8888/smile2/organic/";
echo $page->get_absolute_path();
Works fine for me.
Have you checked that the script and esp. the code in question is executed at all?
E.g. add some unconditional debug-output to the script. Or install a debugger like XDebug to step through the code and inspect variables.
<?php
class PageClass {
var $absolute_path = NULL; // old php4 declaration, see http://docs.php.net/oop5
function get_absolute_path() { // again old php4 declaration
$url = $this->absolute_path;
echo "debug: "; var_dump($url);
echo $url;
}
}
$page = new PageClass();
$page->absolute_path = "http://localhost:8888/smile2/organic/";
echo "debug: page->get_absolute_path\n";
$page->get_absolute_path();