i'm sorry if this is a noob question, but i can't figure it out.
I have a .php file containing a class:
<?php
class Visitors
{
public function greetVisitor()
{
echo "Hello<br />";
}
function sayGoodbye()
{
echo "Goodbye<br />";
}
}
?>
In another php file containing mostly HTML, i try to create an instance of this class and call the function greetVisitor :
<div class="map col-md-4">
<?php
$test = new Visitors();
$test->sayGoodbye();
?>
</div>
For some reason this isn't displaying when i look in my browser. Any ideas ?
tried doing a var_dump, but nothing is showing
thanks
Before you create an object of the class Visitors you need to include/require the php file where that class is defined, like this:
By the way, do not use the parenthesis since you are not sending any values
and yes you are a newbie!
Related
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'; }
}
Guys why doesn't php read my class file? I have this structure:
home.php
stamp.php
class/class.stamp.php
class/class.text.php
home.php
<?php
echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();
include 'class/class.text.php';
?>
class.text.php
<?php
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text);
echo $text;
}
}
?>
The result? Nothing! It says:
Fatal error: Call to a member function something on a non-object in /bla/bla/bla/
But if i do something like that, it works!
home.php
<?php
echo "hello i'm the home page";
include 'class/class.text.php';
?>
class.text.php
<?php
include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');
?>
class.stamp.php
<?php
class Stamp {
public function something($text) {
echo $text;
}
}
?>
But I don't need that, please guys help me :(
Check if the issue is with the directory.
You said it works with class.stamp.php, but shouldn't that be class/class.stamp.php? Here's the thing, if your homepage is located in a certain folder, then in that same folder location you should have the folder class, adjacent to your Home.php file.
I've also had the issue of uninstantiated object, so make sure you are including it in every scope. To help you catch the error, use
require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice
If require doesn't help you solve it, post everything please.
Is it possible to call only the specific function from another file without including whole file???
There may be another functions in the file and don't need to render other function.
The short answer is: no, you can't.
The long answers is: yes, if you use OOP.
Split your functions into different files. Say you are making a game with a hero:
Walk.php
function walk($distance,speed){
//walk code
}
Die.php
function die(){
//game over
}
Hero.php
include 'Walk.php';
include 'Die.php';
class Hero(){
//hero that can walk & can die
}
You may have other functions like makeWorld() that hero.php doesn't need, so you don't need to include it. This question has been asked a few times before: here & here.
One of the possible methods outlined before is through autoloading, which basically saves you from having to write a long list of includes at the top of each file.
In PHP it's not available to get only a little part of a file.
Maybe this is a ability to use only little parts of a file:
I have a class that calls "utilities". This I am using in my projects.
In my index.php
include("class.utilities.php")
$utilities = new utilities();
The file class.utilities.php
class utilities {
function __construct() {
}
public function thisIsTheFunction($a,$b)
{
$c = $a + $b;
return $c;
}
}
And then i can use the function
echo $utilities->thisIsTheFunction(3,4);
include a page lets say the function is GetPage and the variable is ID
<?php
require('page.php');
$id = ($_GET['id']);
if($id != '') {
getpage($id);
}
?>
now when you make the function
<?php
function getpage($id){
if ($id = ''){
//// Do something
}
else {
}
}
?>
I wrote a service class called search_categorization_service.php. Now I am making a call to python scrpt in this class
class SearchCategorizationService
{
function searcher($query)
{
$tmp=passthru("python serverscript1.py $query");
ob_start();
$out=ob_get_contents();
echo print_r($out,true);
}
}
but i dont get any output on the browser. i tried returning it to a controller class and printing the output but it just wont work.any help wud be appreciated. is it an issue with cakephp? because the same application works fine in normal php.
Try moving ob_start() above $tmp=passthru("python serverscript1.py $query");. It appears nothing is being output after the output buffer is started.
<?php
class SearchCategorizationService
{
function searcher($query)
{
ob_start();
$tmp=passthru("python serverscript1.py $query");
$out=ob_get_contents();
echo print_r($out,true);
}
}
?>
I understand that the question is rather hard to understand, I didn't know how to ask it better, so I'll use this code example to make things more clear:
If I have the following files:
test.php:
<?php
include('include.php');
echo myClass::myStaticFunction();
?>
include.php
<?php
__autoload($classname){
include_once("class/".$classname.".php"); //normally checking of included file would happen
}
?>
class/myClass.php
<?php
class myClass{
public static function myStaticFunction(){
//I want this to return test.php, or whatever the filename is of the file that is using this class
return SOMETHING;
}
?>
the magic FILE constant is not the correct one, it returns path/to/myClass.php
in case you need to get "test.php" see $_SERVER['SCRIPT_NAME']
I ended up using:
$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
I am using
$arr = #debug_backtrace(false);
if (isset($arr))
foreach ($arr as $data)
{
if (isset($data['file']))
echo $data['file'];
// change it to needed depth
}
This way you don't need to modify the file from which your file is included. debug_backtrace might have some speed consenquencies.