php functions stopped working [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have an android app sending some data to a php script. Every time my android service sends that data I receive the following error:
Code:
if(isset($_POST['lat']) && isset($_POST['lng']) && isset($_POST['max']))
{
testMe();
function testMe()
{
echo "asdasddasasd";
}
...
}
Error:
05-04 20:05:47.420: D/general(32692): Fatal error: Call to undefined function testMe() in C:\Apache24\htdocs\myApp\reader\getDistance.php on line 7
Why is this wrong?

From the PHP manual documentation:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.
Declare the function before calling it. Try this:
if (isset($_POST['lat']) && isset($_POST['lng']) && isset($_POST['max'])) {
function testMe() {
echo "asdasddasasd";
}
testMe();
}

Related

I get a "Non static method" error after upgrading to PHP 8.1 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I have the following PHP error after upgrading from PHP7.4 to PHP8.1
$result = CarsDAO::selectCarsByColor($idCar, self::CARS_PER_PAGE, 0);
Non static method 'selectCarsByColor' should not be called statically
Any ideas how to rewrite this to be OK?
As the error says, the method is not static in the CarsDAO class, so you should call it on an instance.
$car = new CarsDAO();
$result = $car->selectCarsByColor($idCar, self::CARS_PER_PAGE, 0);
or repair the class by making the method static adding the static keyword in front of the method declaration. Read about static in the manual.
class CarsDAO {
public static function selectCarsByColor($idCar, $carsPerPage, $zeroThing) {
// code here
}
}

Simple PHP code is not working. Whats the reason? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
Just to understand how PHP works in some situations: Why this code is not working?
<?php
class MyCustomClass {
?>
<?php
function hello_world() {
return "Hello World";
}
}
?>
Error: syntax error, unexpected '?>', expecting function (T_FUNCTION) or const (T_CONST) in...
Is it not allowed to close and reopen php tags? Otherwise I see no issues here.
You can NOT break up a class definition into multiple files. You also can NOT break a class definition into multiple PHP blocks, unless the break is within a method declaration. The following will not work:
<?php
class MyCustomClass {
?>
<?php
function hello_world() {
return "Hello World";
}
}
However, the following is allowed:
<?php
class MyCustomClass {
function hello_world() {
?> whatever you want here <?php
return "Hello World";
}
}

PHP 5.3 constructor method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am a Java/C# programmer who is trying to learn/finish a project in PHP.
Can anyone explain to me why "Composition" doesn't work in PHP 5.3 as one would expect from an object oriented language?
I have tried to research the issue, but due to term-confusion (making Google useless...) and bad documentation, I haven't been able to find anything useful yet.
<?php /*PHP VERSION 5.3.3*/
class MyClassOne
{
public function myFunctionOne()
{
echo "<p> My Function One </p>";
}
}
class MyClassTwo
{
private $myClassOne;
function __constructor() // WRONG WRONG WRONG - __construct() - and it works.
{
$this->myClassOne = new MyClassOne();
}
public function myFunctionTwo()
{
echo "<p> My Function Two </p>";
$this->myClassOne->myFunctionOne(); // This crashes the "application"
}
}
$myclassone = new MyClassOne();
$myclassone->myFunctionOne();
$myclasstwo = new MyClassTwo();
$myclasstwo->myFunctionTwo();
/*
Expectet result:
My Function One
My Function Two
My Function One
Real result:
My Function One
My Function Two
(application/runtime crash)
*/
?>
It will be highly appreciated if anyone can provide an explanation or show me the relevant documentation for this behavior.
You have a typo. Change __constructor to __construct and it will work correctly.
PHP constructors should be named __construct() as per the documentation.

PHP - New to OOP, already stuck [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am getting started with OOP because I already wrote procedural code enough that I want to step up.
I started by creating a file 'user.class.php', wrote some code, and loaded in into the server.
<?php
class user {
private $name;
private $age;
function __construct($name, $age){
$this->name=$name;
$this->age=$age;
}
function getName() {
return $this->name;
}
function getAge() {
return $this->age;
}
}
$usr = new user('Alex', 16);
print($usr->getName.'<br>');
print($usr->getAge.'<br>');
I ran the code on the web server and got the following error:
Notice: Undefined property: user::$getName in /Library/WebServer/Documents/user.class.php on line 24
Same happened with the getAge function call.
If I can get this code working I will feel better by starting to write more OO code instead of just procedural code. Thanks in advance.
You're just missing your parenthesis for your method calls. Without them you are trying to get a property called getName which doesn't exist:
print($usr->getName().'<br>');
print($usr->getAge().'<br>');
$usr->getName means member variable
where you have a function so
$usr->getName()
is the correct way to call the function for that object
You are missing parenthesis in
print($usr->getName().'<br>');

Storing reference to other class object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I come from a java background and m trying a hand at php. Right now I m trying to pass a object to constructor of a class and trying to store a reference to it inside the class and upon a function call to this call execute a method from the stored reference.
$phpBook = new Book("Php Book", 500);
$vihaan = new Person("Vihaan", $phpBook);
Person.php
class Person
{
private $_book;
private $_name;
public function __construct($name, $book)
{
$_this->_book = $book;
$_this->_name = $name;
}
on this line
$_this->_book = $book;
I get a warning.
PHP Warning: Creating default object from empty value in /home/vihaan/workspace/AdapterPattern1/Person.php on line 12
and this function call never enter the if block as $_book seems to be empty.
public function openBook($pageNumber = 0)
{
if(!empty($_book))
{
$_book->open($pageNumber);
}
}
It's called $this, not $_this!
$this->_book = $book;
$this->_name = $name;

Categories