I've been struggling for a few days now with a completely weird bug:
Here's the scenario (bear with me):
I have one "framework" class, which I'll call F.
I have some simple classes that extend F, one of them I'll call P.
So what I have is:
class F {
[...]
protected static $_tabela;
[...]
final public static function _Tabela() {
return static::$_tabela;
}
public static function Consultar() {
echo static::_Tabela();
}
}
class P extends F {
protected static $_tabela = 'produtos';
}
And when I call P::Consultar(); I get this error that makes no sense to me:
Fatal error: Undefined class constant 'self::STRING' in [...]/F.inc.php on line X
Where X is the body of the _Tabela() method.
So, I've tried changing the variable name ($_tabela).
I tried saving the class name via get_called_class():
$class = get_called_class()
return $class::$_tabela;
But got the same error.
Also, the error message is completely useless, I'm not trying to access a class constant, but instead a class static property!
Googling the error message also gave me no useful results.
Edit: Thanks everyone for the answers!
I found the problem, and it had nothing to do with the code I was looking at.
Turns out there was an error in the definition of the P class, so when I tried calling static::Consultar, PHP parsed the class and complained about the error!
If you're using PHP version >= 5.3.0 , you can do this:
<?php
class F {
protected static $_tabela = 'a';
final public static function _Tabela() {
$s = new static();
return $s::$_tabela;
}
public static function Consultar() {
$s = new static();
echo $s::_Tabela();
}
}
class P extends F {
protected static $_tabela = 'produtos';
}
echo P::Consultar(); // echos 'produtos'
Related
I'm new to PHP and I am trying to follow a course.
<?php
class myCar {
function myCar() {
$this->model = "Sports";
}
}
$Range_Rover = new myCar();
echo $Range_Rover->model;
?>
According to the tutorial I should get the output "Sports" What is causing the code to result in an error of undefined property?
Warning: Undefined property: myCar::$model in
C:\xampp\htdocs\Test\index.php on line 16
Screenshot of tutorial results
Many thanks,
Stuart
Correct way to do it:
<?php
class myCar {
public $model;
function __construct() {
$this->model = "Sports";
}
}
$Range_Rover = new myCar();
echo $Range_Rover->model;
Output: https://3v4l.org/bnNrm
Note: Why above is needed
a) Defining constructor name as class name is an older practice, and it will give you deprecation message for some php version (php 7 versions).
b) Also from php8 it will be treated like a normal function, not as a constructor, so your purpose will not solve.
You need to declare the class variable before using it.
Just add "public $model;" before function mycar().
You should declare model like this
public $model;
Also as some answers told before, the constructor needs to be defined as __constructor now. this would look like
<?php
class myCar {
public $model;
function __constructor() {
$this->model = "Sports";
}
}
$Range_Rover = new myCar();
echo $Range_Rover->model;
?>
I am trying to make an implementation of the Bridge Design Pattern, following the steps on Tutorials Point. I am converting the code from Java to PHP and changing some names.
The problem is, when I try to pass the concrete bridge implementer class to the concrete class implementing interface, an error is throw.
My code is as follows:
// LaunchApi.php
interface LaunchApi
{
public function launch();
}
// RedEngine.php
class RedEngine implements LaunchApi
{
public function launch()
{
echo "The red engine is really fast!!!";
}
}
// Rocket.php
abstract class Rocket
{
protected $launchApi;
protected function __construct($launchApiImplementer)
{
$this->launchApi = $launchApiImplementer;
}
public abstract function launch();
}
// FullRocket.php
class FullRocket extends Rocket
{
public function __construct($launchApi)
{
parent::__construct($launchApi);
}
public function launch()
{
$this->launchApi->launch();
}
}
// LaunchingScript.php
$redEngine = new RedEngine();
$redEngine->launch(); // this works
$redRocket = new FullRocket($redEngine);
$redRocket.launch(); // this won't work
The error throw is:
design-patterns\Bridge>php LaunchingBridge.php
The red engine is really fast!!!
Fatal error: Call to undefined function launch() in \design-patterns\Bridge\LaunchingBridge.php on line 24
I tried to pass by reference using the &, but it only changes the error.
yeah should be $redRocket->launch(); instead of $redRocket.launch();
like what nigel ren said
I have a class like this:
// file /models/person.php
class Person
{
public function create_path()
{
self::log();
path_helper($this); //a global function in other php file
}
public function log()
{
echo "trying to create a path";
}
}
This is the way how Person is instanciated:
//file /tools/Builder.php
include('/models/Person.php');
class Builder
{
public function build()
{
$type = 'Person';
$temp = new $type();
$temp->create_path();
}
}
As you note in Person class, I am calling the object in question with $this reference. But this is not correct because an error is showed:
Message: Undefined variable: this
I suppose that $this reference point to other object or it is unable to work because the object is created from another script. Also, I tried to use self because there was not problem calling methods with that, but as parameter I get:
Message: Use of undefined constant self - assumed 'self'
So, can you guide me to the right direction?
I tested your code out for myself, with a few minor changes. It appears to work properly.
Changed self::log() to $this->log()
Added global function path_helper (I have no idea what this does)
PHP
function path_helper(Person $object)
{
var_dump($object);
}
class Person
{
public function create_path()
{
$this->log();
path_helper($this); //a global function in other php file
}
public function log()
{
echo "trying to create a path";
}
}
class Builder
{
public function build()
{
$type = 'Person';
$temp = new $type();
$temp->create_path();
}
}
$Build = new Builder();
$Build->build();
Result
trying to create a path
object(Person)[2]
Your code is correct and your going in the right direction.
You should call the log method like this:
$this->log();
because using self:: is reserved for static methods.
Also, try calling the path_helper function like this:
path_helper(self);
Hope I could help you. Couldn't test it, but it should work.
I have a class defined which has several constants defined through `const FIRST = 'something';
I have instantiated the class as $class = new MyClass()
then I have another class that takes a MyClass instance as one of it's constructors parameters and stores it as $this->model = $myClassInstance;
This works fine.
But I am wondering how I can access the constants from that instance?
I tried case $this->model::STATE_PROCESSING but my IDE tells me
Incorrect access to static class member.
and PHP tells me
unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in ...
I know I can do MyClass::STATE_PROCESSING but I am wondering if there is a way to get them based off the instance?
Seems like your on an older version of php? PHP 5.3 allows the access of constants in the manner you describe... However, this is how you can do it without that inherent ability:
class ThisClass
{
const FIRST = 'hey';
public function getFIRST()
{
return self::FIRST;
}
}
class ThatClass
{
private $model;
public function setModel(ThisClass $model)
{
$this->model = $model;
}
public function getModel()
{
return $this->model;
}
public function Hailwood()
{
$test = $this->model;
return $test::FIRST;
}
}
$ThisObject = new ThisClass();
echo $ThisObject ->getFIRST(); //returns: hey
echo $ThisObject ::FIRST; //returns: hey; PHP >= 5.3
// Edit: Based on OP's comments
$ThatObject= new ThatClass();
$ThatObject->setModel($Class);
echo $ThatObject->getModel()->getFIRST(); //returns: hey
echo $ThatObject->Hailwood(); //returns: hey
Basically, were creating a 'getter' function to access the constant. The same way you would to externally access private variables.
See the OOP Class Constants Docs: http://php.net/manual/en/language.oop5.constants.php
I have a ready made class X
class X
{
private $pA;
function __construct($id=0)
{
$pA=new myClass($id);
}
}
class myClass
{
private $id;
function __construct($id=0)
{
echo 'constructing....';
}
}
But there is no echo output the class construction stops at the new operator.
sorry there is () after myclass I mistook
Parse error: parse error, expecting `'{'' in test.php on line 11
change
class myClass()
to
class myClass
and actually do new X(); and it will work as expected.
you might also want to set error_reporting = E_ALL and display_errors = 1 in your php.ini when developing or debugging code to see whats wrong.
It worked for me when I took out the () after myClass() like this:
class myClass
{
private $id;
function __construct($id=0)
{
echo 'constructing....';
}
}
Have you actually tried creating an object of type X?
Also, you address class members using $this
$this->pA = new myClass($id);
And the error that #Basti pointed out should be addressed as well.
GL~