I'm having trouble calling a static method in the correct location. It always shows at the beginning of the html. As in such:
function topnav()
{
echo "<div class=\"col-md-3\">".Site_Config::url()."</div><div class=\"col-md-3\">".Site_Config::title()."</div>";
}
Shows in the source of the page as:
https://www.example.comExample Title<div class="col-md-3"></div><div class="col-md-3"></div>
I'm not sure why this happens or if there is a way to fix it. Any help is appreciated.
Looks like you echoing the result of url function. Instead, return it.
class Site_Config {
public static function url() { return 'some_url'; }
}
Related
I am creating website in PHP. I am using MVC in PHP. My website works like this, if user go to example.com/about then it it will load About class and index() function. If user will go to localhost/about/founder then it will load founder() function from About class. but the thing is that if I go to localhost/About or localhost/AbOut or anything like that it is loading default index() function from About class file. So what to do with case sensitivity? I mean I want my script to load index() function from class file if it is localhost/about or localhost/terms. If anything is in uppercase, then it should load 404 error function. 404 error function is already set in my site.
Please help me friends.
here is my Bootstrap.php class file
<?php
/*
Bootstrap class to run functions by URL
*/
class Bootstrap {
public $_req;
public $_body;
public $_file;
public $_error;
function __construct(){
if(empty($_GET['req'])){
require 'classes/home.php';
$this->_body = new Home();
$this->hdr($this->_body->head());
$this->_body->index();
$this->ftr();
exit();
}
$this->_req = rtrim($_GET['req'], '/');
$this->_req = explode('/', $this->_req );
$_file = 'classes/'.$this->_req[0].'.php';
if(file_exists($_file)){
require $_file;
}
else {
$this->error(404);
}
$this->_body = new $this->_req[0];
$this->hdr($this->_body->head());
if(isset($this->_req[2])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}($this->_req[2]);
}else {
$this->error(404);
}
}else {
if(isset($this->_req[1])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}();
}else {
$this->error(404);
}
}else {
$this->_body->index();
}
$this->ftr();
}
}
//this function is to set header in html code
public function hdr($var = false){
echo '<!DOCTYPE HTML><html><head>'.$var.'</head><body>';
}
//this function is tp set footer in html code
public function ftr($var = false){
echo $var.'</body></html>';
}
//error handler
public function error($var){
require 'classes/er_pg.php';
$this->_error = new Error();
$this->_error->index($var);
}
}
You shouldn't use anything to load non-lowercase URLs because of the duplicate content, and that's a good thing you're doing. The wrong URLs should fail automatically in such cases.
However, since you didn't show how are you making those calls, then only thing I can suggest at this point is to check if the called method exists (case-sensitive), and if not, throw/redirect to a 404 page (header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");).
UPDATE
After all the chat in the comments, seems like file_exists is not case-sensitive in your case, which is really weird. Hopefully someone will be able to figure it out so I can delete this (keeping it because of the info in the comments).
I solved the problem. I used this
if(ctype_lower($this->_req[0])){
$_file = 'classes/'.$this->_req[0].'.php';
and now its working. Thanx anyways friends.
I have set in myFile.php this function:
function monthLanguage()
{
if ($this->lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
}
I was thinking to wrap this if statement into a function to call it where is needed as a kind of short code.
I call it like this:
monthLanguage();
but I get error message: Call to undefined function
Any help on how to reach my short code intent?
Are you including the monthLanguage function the file you are using it in? Also, I spotted two issues with this code. You are not initiating the array called $dayName and nothing is being returned so the function will not send back output. It should be like this.
function monthLanguage()
{
$dayName = array();
if ($this->lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
return $dayName;
}
Also, the $this is not clear since that is usually used in the scope of a class, so perhaps you need to set the function like this:
function monthLanguage($lang)
{
$dayName = array();
if ($lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
return $dayName;
}
And you would then call the function in PHP like this:
monthLanguage($this->lang);
Or like this:
monthLanguage($lang);
But it is unclear where this function is being placed or used, so clarify that to decide which is the best way to handle.
I don't think you can use $this->lang - it's usually reserved for a method if I'm not mistaken.
Try replacing the function with this, should work like a charm.
function monthLanguage($lang) {
if ($lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
}
newbie in PHP here, sorry for troubling you.
I want to ask something, if I want to include a php page, can I use parameter to define the page which I'll be calling?
Let's say I have to include a title part in my template page. Every page has different title which will be represented as an image. So,
is it possible for me to call something <?php #include('title.php',<image title>); ?> inside my template.php?
so the include will return title page with specific image to represent the title.
thank you guys.
An included page will see all the variables for the current scope.
$title = 'image title';
include('title.php');
Then in your title.php file that variable is there.
echo '<h1>'.$title.'</h1>';
It's recommended to check if the variable isset() before using it. Like this.
if(isset($title))
{
echo '<h1>'.$title.'</h1>';
}
else
{
// handle an error
}
EDIT:
Alternatively, if you want to use a function call approach. It's best to make the function specific to activity being performed by the included file.
function do_title($title)
{
include('title.php'); // note: $title will be a local variable
}
Not sure if this is what you're looking for, but you can create a function to include the file and pass a variable.
function includeFile($file, $param) {
echo $param;
include_once($file);
}
includeFile('title.php', "title");
In your included file, you could do this:
<?php
return function($title) {
do_title_things($title);
do_other_things();
};
function do_title_things($title) {
// ...
}
function do_other_things() {
// ...
}
Then, you could pass the parameter as such:
$callback = include('myfile.php');
$callback('new title');
Another more commonly used pattern is to make a new scope for variables to be passed in:
function include_with_vars($file, $params) {
extract($params);
include($file);
}
include_with_vars('myfile.php', array(
'title' => 'my title'
));
The included page will already have access to those variables defined prior to the include. If you require include specific variables, I suggest defining those variables on the page to be included
I have this in my class
When the second function is called php errors with
wrong datatype and only variables can be past by reference.
I don't know what they mean by that
This code comes from php.net
If the same code is outside the class it executes fine
What am I doing wrong here, if I am working within a class?
$extensiesAllowed= array();
function __construct() {
$this->extensiesAllowed= array("txt", "pdf");
$this->fileName= $_FILES['file'];
}
private function isAllowedExtensie($fileName) {
return in_array(end(explode(".", $fileName)), $this->extensiesAllowed);
}
public function check_upload() {
if($this->fileName['error'] == UPLOAD_ERR_OK) {
if(isAllowedExtensie($this->fileName['name'])) {
return true;
}
}
}
the php error shows
Array
(
[bestandsNaam] => ACCOUNT INFO.txt
[extensiesAllowed] =>
)
Thanks, Richard
try putting the end and explode in seperate statements - I think end() may read by reference. In any case, it will help you figure out what line is causing you problems if it doesnt fix it.
In the second function/method you should call should be calling isAllowedExtensie as $this-> isAllowedExtensie()
if($this->isAllowedExtensie($this->fileName['name'])) {
Edit: forget my second comment..
Given this class:
class Tacobell{
public function order_taco(){
echo "3 Tacos, thank you.";
}
public function order_burrito(){
echo "Cheesy bean and rice, please";
}
}
$lunch = new Tacobell;
$lunch->order_burrito();
$lunch->order_taco();
How would I do something like this?
$myOrder = 'burrito';
$lunch->order_.$myOrder;
Obviously that code is bunk--but shows what I'm attempting to do better than trying to explain it away.
And maybe I'm going about this all wrong. I thought about a method with a switch statement, pass in burrito or taco, then call the right method from there. But then I have to know the end from the beginning, and I may potentially have lots of methods and I'd rather not have to update the switch statement everytime.
Thanks!
How about something like this?
class Tacobell {
public function order_burrito() {
echo "Bladibla.\n";
}
public function order($item) {
if (method_exists($this, "order_$item")) {
$this->{'order_' . $item}();
} else {
echo "Go away, we don't serve $item here.\n";
}
}
}
You would call it using $lunch->order('burrito');, which looks much cleaner to me. It puts all the uglyness in the method Tacobell::order.
$lunch->{'order_' . $myOrder}();
I do agree the design is a little iffy, but that's how to do it at least.
I think call_user_func is what you're looking for:
http://us3.php.net/call_user_func
You can pass it the string you suggested. See example #3 for calling a method of a class.
simple enough
$order = 'order_burrito';
$lunch->$order();