mail function not working in OO PHP - php

I tried the following:
a.php
<?php
class Page
{
function go(){
echo "1";
send();
}
function send(){
mail("whatever#gmail.com","subj","hi");
}
}
?>
b.php
<?php
require("a.php");
$page=new Page();
$page->go();
?>
b.php doesn't neither sends a mail nor t echo anything. When I put echo before send in the function go(), PHP echo "1" but doesn't send anything. I thought maybe there's something wrong with the mail() function so I changed b.php to:
<?php
require("a.php");
$page=new Page();
$page->send();
?>
and everything works fine. What is the problem with the initial code?

This isn't a mail function. You are trying to call a function that isn't labeled correctly. In this type you're attempting to call a static function. But the function you're calling is not labeled static. You can fix this a few different ways however.
Your echo doesn't work because it can't find the function you're referencing in go().
// best use
class Page{
public function go(){
echo "yay";
$this->send();
}
...
}
or
// static isn't recommended most often, but this will work.
class Page{
// same go function
public static function send(){
// same
}
}

Been awhile since I've used PHP, but you have to use $this to call the send method from a class method. See below:
<?php
class Page
{
function go(){
echo "1";
$this->send(); # Added the $this-> to the function call
}
function send(){
mail("whatever#gmail.com","subj","hi");
}
}
?>

Change send();
to
$this->send();
send() is a class method not a function, hence it cannot be called without $this in this case.

Related

PHP function working differently for return and echo. Why?

By using the following class:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
$trimmed=trim($form);
$specialchar=htmlspecialchars($trimmed);
$finaloutput=stripslashes($specialchar);
echo $finaloutput;
}
public function __destruct()
{
unset($finaloutput);
}
}
and Calling the function, by the following code, it works fine.
<?php
require('source/class.php');
$target="<script></script><br/>";
$forminput=new SafeGuardInput($target);
?>
But if in the SafeGuardInput class if I replace echo $finaloutput; with return $finaloutput; and then echo $forminput; on the index.php page. It DOES NOT WORK. Please provide a solution.
You can't return anything from a constructor. The new keyword always causes the newly created object to be assigned to the variable on the left side of the statement. So the variable you've used is already taken. Once you remember that, you quickly realise there is nowhere to put anything else that would be returned from the constructor!
A valid approach would be to write a function which will output the data when requested:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
}
public function getFinalOutput()
{
$trimmed = trim($this->form);
$specialchar = htmlspecialchars($trimmed);
$finaloutput = stripslashes($specialchar);
return $finaloutput;
}
}
Then you can call it like in the normal way like this:
$obj = new SafeGuardInput($target);
echo $obj->getFinalOutput();

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.

Why function is undefined in ajax file - does function order matter in AJAX file?

I am really very surprised to see my function comes out to be undefined on AJAX request. I am wondering does function order matters in AJAX file? Following code will show you my problem -
This is the ajax.php file which is called by jquery/ajax request from index.php. It is supposed to simply print the name -
<?php
if(isset($_POST))
{
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
}
?>
But when this file is called, i get -
Fatal error: Call to undefined function display_name()
But when i change the order of code i.e. function like this -
<?php
if(isset($_POST))
{
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
}
?>
then it displays -
admin
How strange it is!
Thus if really function availability order matters then how the following code works -
It is simple file and it is NOT called by AJAX request. I am simply loading the file and doesn't matter where the function is written. It is working by all using any order of line code -
<?php
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
?>
The following snippet is also working -
<?php
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
?>
So please tell my the reason behind this difference. In page loading code works and on ajax request it doesn't. Why the function display_name() is undefined if it exists?
This has nothing to do with Ajax.
From the PHP manual:
Functions need not be defined before they are referenced, except when a function is conditionally defined
The order matters in your first example because the function is inside an if statement.
I think this issue is not related to calling file as Ajax request. This is more related to PHP's function declaration scope.
Consider the following situation:
if(true)
{
function bar()
{
}
$functions = get_defined_functions();
print_r($functions["user"]);
}
function foo()
{
}
this code will give bar and foo as defined functions.
But the following situation produces just foo function:
if(true)
{
$functions = get_defined_functions();
print_r($functions["user"]);
function bar()
{
}
}
function foo()
{
}
From this we see that all the function in a file are defined and available imediately when the file is loaded, but if blocks are interpreted as the execution passes step by step.
Hope this helps.

Having problems on fetching variables on a class in PHP

I just want to ask if its possible to call variables on class to another page of the site. I have tried calling the function's name and inside the parenthesis. I included the variable found inside that function e.g:
<?php
$loadconv -> loadmsg($msgReturn);
echo $loadconv;
?>
But it didn't work.
Do you want something like this?
class Load
{
public $msgReturn;
__construct()
{
}
public function loadMsg($param)
{
$this->msgReturn = $param;
}
}
Then you could do
$loadConv = new Load();
$loadConv->loadMsg('just a string');
echo $loadConv->msgReturn; // 'just a string'

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.

Categories