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))
Related
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
}
}
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 5 years ago.
Improve this question
I have the following class structure. When run the post() function, I receive an error Uncaught Error: Call to undefined function curl_post().
class POST {
private function encode($data) {
}
private function curl_post($data) {
encode($data);
}
public function post($data) {
$post = curl_post($data);
print_r($post);
}
}
What is causing this error? Do I have to use something like $this-> to access the private function?
To call sibling object methods, prefix the call with $this->:
class POST {
private function encode($data) {
}
private function curl_post($data) {
$this->encode($data);
}
public function post($data) {
$post = $this->curl_post($data);
print_r($post);
}
}
From the documentation:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs...
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.
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>');
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;