Good morning everybody, I have a two class, the first accesses the second class.
More on segunta class has a private $ my, and this gives the error Undefined property: session::$my in line, if($this->my)
I would be very grateful for the help.
Sample code,
class session{
public function run_session(){
..run..
data::run($line);
}
}
class data {
private $my = "../../my/";
public function run($line){
if($this->my.$line){
....run...
}
}
}
you must use like this
class data {
private $my = "../../my/";
public function run($line){
if($this->my.$line){ // here you are using $this, so the function must be called on object of class data
....run...
}
}
}
class session{
public function run_session(){
..run..
$data = new data(); // create object of class data, so that you can call the function run
$data->run($line);
}
}
Related
Take the following example of two classes:
class Yolo {
public function __invoke() {
echo 'YOLO';
}
}
class Swag {
public $yolo;
public function __construct() {
$this->yolo = new Yolo();
}
}
Is it possible to invoke the Yolo object via an instance of Swag?
(new Swag())->yolo(); throws a warning and doesn't call __invoke:
PHP Warning: Uncaught Error: Call to undefined method Swag::yolo()
In PHP 7 you can directly call it (just need extra braces):
((new Swag())->yolo)();
In PHP 5 you need a temp variable:
$y=(new Swag())->yolo;
$y();
The problem is that you try to call the function yolo() on the Swag class. In your case you have to use the public class variable and call the subclass over that.
var_dump((new Swag())->yolo);
that is your object. When you use () you try to call the class not the class variable.
You do not even have to make it a public method.
But you might have to reroute the call manually since the callable method is created at runtime:
class Yolo {
public function __invoke() {
echo 'YOLO';
}
}
class Swag {
private $yolo;
public function __construct() {
$this->yolo = new Yolo();
}
function __call($method, $args) {
if(is_callable($this->$method))
{
return call_user_func_array($this->$method, $args);
}
}
}
(new Swag())->yolo();
It would probably a good idea to check first, if the called method exists and if it is actually callable.
But the example works as a proof of concept.
I don't understand what is the different between private method and protected method in Object Oriented PHP. After make a method private , I'm able to access it from extends class. Please check the code below -
<?php
class person{
private function namedilam(){
return "likhlam";
}
public function kicu(){
return $this->namedilam();
}
}
class second extends person{
}
$info = new second;
echo $info->kicu();
The difference will become clear when you do it like this:
class Liam {
private getFirstName() {
return "Liam";
}
public function getName() {
return $this->getFirstName();
}
}
class Max extends Liam {
private function getFirstName() {
return "Max";
}
}
class Peter extends Liam {
public function getLiamsName() {
return $this->getFirstName();
}
}
$max = new Max();
echo $max->getName();
// returns "Liam", not "Max" as you might expect
$peter = new Peter();
echo $peter->getLiamsName();
// PHP Fatal error: Uncaught Error: Call to private method Liam::getFirstName() [...]
Max will return "Liam" because the getName() calls getFirstName() in the Liam class, not the one from the class extending it. This means with private methods you can make sure that whenever in your class you call this method exactly this one is used and it will never be overwritten.
To explain it in general terms:
private methods are only accessible inside the class. They can not be overwritten or accessed from outside or even classes extending it.
protected methods are accessible inside the class and in extending classes, but you can't call them from outside like:
$max = new Max();
$max->iAmProtected();
This will neither work with private or protected methods.
I'm trying to access an object of a class in another extended class.
class MainClass{
protected $theobject;
function __construct(){
$this->theobject = new AnotherClass();
}
}
class TheClass extends MainClass{
function AnotherFunction(){
$this->theobject->SomeFunction();
}
}
I'm getting an error on $this->theobject->AnotherFunction(). The error is "Call to a member function SomeFunction() on a non-object".
But this works fine:
class TheClass{
protected $theobject;
function SoemFunction(){
$this->theobject = new AnotherClass();
$this->theobject->SomeFunction();
}
}
Is it even legal to do this in PHP?
Pretty much all the code: http://3v4l.org/MaXe6
When i var_dump the $this->theobject in TheClass is comes back as NULL.
class TheClass extends MainClass{
function __construct(){
parent::__construct();
}
function SoemFunction(){
$this->theobject->SomeFunction();
}
}
Do the construct method when init the child class.
This is a very silly question, but I don't know what's wrong. I can't get the value of a private variable through a public method. I'm using CodeIgniter.
class someClass extends MY_Model {
private $table = 'sometable';
public function getTable() {
return $this->table;
}
public function updateTable($data) {
$this->db->update($this->getTable(), $data);
}
}
When I call this method from the controller, I get this message:
Fatal error: Access level to someClass::$table must be public (as in class MY_Model) in /some/path/someclass.php on line 38
What have I done wrong? Thank you.
Your parent class MY_Model is declaring that field with public scope, so you must adhere to that in your child class.
--- A.php ----
require_once 'B.php';
class A
{
public function __constructor($x){...}
public function foo()
{
$b = B::getInstance();
...
}
}
--- B.php ----
require_once 'A.php';
class B extends A
{
protected static $_instance = null;
protected function __construct(){}
public static function getInstance()
{....}
}
PHP just stops interpreting the code as soon as it reaches line
protected function __construct(){}
and outputs everything before and nothing that would have been sent to the browser afterwards.
And as soon as I just take that line out, by changing it to
// protected function __construct(){}
everything works fine!?
I don't get that.
Any ideas?
I've just created a simple test-file to confirm whether this happens on my machine too, and I think I've found the answer. Take the following code:
<?php
error_reporting( E_ALL | E_STRICT );
class Foo {
public function __construct( ) {
}
}
class Bar extends Foo {
protected function __construct( ) {
}
}
When trying to execute that code, I get a fatal error: "PHP Fatal error: Access level to Bar::__construct() must be public (as in class Foo) in /home/berry/foo.php on line 12." That means you can't change the access level in a child class, if the parent has already defined the access level, which actually makes a lot of sense: PHP wouldn't know which constructor to call, I guess.
As a side note: by looking at your code, B extends A, and A uses B. Why exactly is that so, it seems like a strange construction to me? I'm guessing you actually want is composition, not inheritance.
You can define a constructor as protected or private. This code compiles runs just fine since OOP was rewritten for PHP/5:
<?php
class A{
public function __construct(){
echo 'Instance of A created' . PHP_EOL;
}
}
class B{
protected function __construct(){
echo 'Instance of B created' . PHP_EOL;
}
}
class C{
private function __construct(){
echo 'Instance of C created' . PHP_EOL;
}
}
Of course, private constructors prevent you from creating an instance with the new keyword, but PHP will trigger a fatal error (it won't just stop running):
<?php
new A; // OK
new B; // Fatal error: Call to protected B::__construct() from invalid context
new C; // Fatal error: Call to private C::__construct() from invalid context
You can create custom static initiators:
<?php
class FancyClass {
public static function init() {
new self();
}
protected function __construct() {
echo 'Instance created!';
}
}
# This will not work.
new FancyClass();
# But this will work.
FancyClass::init();