Simple PHP code is not working. Whats the reason? [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 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";
}
}

Related

Why isnt "new" throwing error if class is not declared in PHP? [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 6 years ago.
Improve this question
Lets have two files: index.php and foo.php
The foo.php file would be placed in subdirectory /lib
now in index.php, lets have this function
function bar(){
require('foo.php');
$foo = new Foo();
$foo->bar();
}
And in foo.php
class Foo{
public function __construct(){
echo "baz";
}
public function bar(){
echo "bar";
}
}
Now, the error is hard to spot: I made error in requiring the class Foo, because instead require('lib/foo.php') I wrote only require('foo.php')
That error is really hard to spot, especially if you have many lines of code.
Now should declaration of class throw an error?
If I would get error on the line $foo = new Foo(); the error would be easy to spot. In this case you get Fatal error: Call to undefined method Foo::bar() which makes you think: "But I declared the method" and you forget that the error ight be somewhere else.
So, why does not PHP throw any error on creating new class which does not exist?
PHP does throw a fatal error when you are trying to instantiate a class which is not currently loaded and is not autoloadable either. The most likely explanation for your situation is that you do have a class Foo declared and included somewhere, but it's not the class Foo you expect.
In fact, the require call should have died first before new was even reached. You do have another class Foo in foo.php.

abstract class cannot be declared as public? [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 writing a code in drupal custom module
It throws error while adding public keyword before abstract
public abstract class testParent {
public function abc() {
return 1;
}
}
// Class to extent abstract class property
class testChild extends testParent {
public function xyz() {
//body of your function
}
}
$a = new testChild();
print $a->abc();
The members of a class may be limited in visibility, but all classes are essentially 'public' in PHP.
http://php.net/manual/en/language.oop5.visibility.php
You will get a syntax error if you try to use the public keyword on a class (something like PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC))

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 functions stopped working [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 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();
}

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>');

Categories