I have a PHP class which I'm instantiating an object for. If I change the namespace of the class of change the path of the class, the autoloader complains that that class is not defined. But when I do everything correctly, there are no complaints but the echo in the constructor is not triggered. So I'm assuming it found the class, but then why is the constructor code not triggered? How can I definitely tell that an object has been instantiated?
$obj = new MyClass()?
How to check whether $obj is instantiated properly?
For the original question, please see here. spl_autoload_register issue while loading class
Any answers you can provide there is also highly appreciated. I'm trying to break a bigger problem into several smaller ones to solve it.
Related
I want to research the source code of pyrocms, and when I read the Base.php, I can't understand the following code
new CI;
the file is system/cms/libraries/Base.php
My problems are
why there has no a variable name, like $CI = new CI;
why it can be used as CI::$APP->config->item('controller_suffix') in it's sub class MX_Controller since there does not have variable name?
Thank you very much!!!
This object isn't stored in a variable because it seems we don't need to manipulate it. On the other hand, look at its constructor: it does a lot of things (since it also calls the constructor of CI_Controller, which in turns loads a Loader and initializer it, ....)
So, we don't build it in order to manipulate it afterwards, but in order to run the code in its constructor.
We can use CI::$APP-> whatever because $APP is a static member, hence it doesn't require to have an instance of CI to be manipulated
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).
see statics on php.net
I am trying a very simple thing here but it doesn't seem to work. Take a look at this code:
include 'custom/mainclass.php';
$child = new childClass();
class childClass extends mainClass {
}
Apparently childClass() cannot be found (According to php).. I'm 100% sure I'm doing something very stupid in the way of ordering my code.
I already searched the web but from what I understand I'm doing nothing wrong..
You have to declare your classes in your code first before using them.
include 'custom/mainclass.php';
class childClass extends mainClass {
}
$child = new childClass(); //Create an instance after the class has been declared
EDIT:
After some research it turned out, that you can actually use a class before declaring it.
But, declaration of the class and all parent classes must be in the same file.
So if you declare a parent class in one file and a child class in another, it won't work.
Also, you must declare parent classes first. After that you can extend them.
Same as with Variables, Functions or other language constructs,
First declare, then use.
I've created a class called XMLParser which is being inlcuded using an auto include like all the other classes I'm working with.
When I try to instantiate an object using this class, I get the fatal error in the title.
The auto-include function works. Changing the class name makes it work, I'm also quite positive this had been working when I set it up, otherwise the unit tests depending on this class would never have passed.
Is there a built in XMLParser class that could be conflicting with this?
Surely, even if there was that would not be a problem as I'd get a declaration error or the object would just be instantiated anyway?
Any light on the matter would really help. It's frustrating to say the least.
There is a PEAR file called XMLParser.php
This file appears in the PHP include path before my class directories.
When the autoload function requires_once XMLParser.php it finds this file, which rightfully, does not have the XMLParser class defined within.
That's the source of the fatal error.
To avoid this, one should probably namespace their classes.
Shame on me, thanks for all your feedback.
I'm using CI and I have a UserModel that selects the user based on login information and sets a userVO and add this userVO in a session like this:
$this->session->set_userdata('user', $userVO);
When I try to access this session it return me this error:
Message: main() [function.main]: The script tried to execute a method
or access a property of an incomplete object. Please ensure that the
class definition "UserVO" of the object you are trying to operate on
was loaded _before_ unserialize() gets called or provide a __autoload()
function to load the class definition.
I have found a "solution", I need CI to load the UserVO class before session class and it works.
The problem is that I have lots os VO classes and I'll need them inside the session and is a bad thing to autoload them because I won't need them all at the same time.
Is there any workaround?
Thanks in advance for any help.
Whats going on is that you are saving an instance of the class to the session. In order to restore it, you first need to load the base class it is an instance of. You likely have lots of "instances" of the VO class, rather than lots of VO classes. You just need to load the file that has the class declaration.
The class instance really only contains what has changed from the base class, not the whole class. So it needs the underlying class to know what the "defaults" are.
I assume $userVO is a Model
$this->session->set_userdata('user', $userVO);
use
$this->session->set_userdata('user', json_encode($userVO));
I'd like to get the names of all classes within a specific namespace in PHP. Currently, I'm attempting to do some magic via reflection on a specific list of named classes, but I'd like to do it without knowing the names ahead of time.
I've tried calling get_declared_classes(), but for whatever reason, classes that I do have available are not showing up. I can call get_declared_classes(), not see Event in the list, then immediately call $x = new Event() without a problem. Something like the following, which I would think should cause a problem...
if (! in_array('Event', get_declared_classes())) { $x = new Event(); }
...works fine. I'm wondering if namespacing these classes and retrieving that way would help alleviate the problem. Is this possible?
EDIT: For clarification, let me add that I am not currently using namespaces, and I am not specifically trying to achieve something from the above listed code. What I want is to get the names of all classes I have declared. Despite the fact the class declarations for all of them are being hit before I call get_declared_classes(), they are not all appearing in the list. I was hoping that namespacing might help solve the problem.
EDIT2: Several people have pointed out that the classes may be autoloaded. I tested this by doing the following. echo(class_exists('Event')) returned a value of 1. echo(class_exists('Event', FALSE)) returned a value of 0. The second, optional parameter to class_exists is whether or not to autoload. So, apparently the class is being autoloaded. That answers that.
So, next question - how do I prevent this? I'm using a framework that really doesn't give me much low-level control. Is there a way to force autoloading, THEN call get_declared_classes, or for get_declared_classes to fire an autoload first?
You do not need to hard code it in the code, you can use variable name:
$class_name = 'Event';
if (!in_array($class_name, get_declared_classes())) {
$x = new $class_name();
};
See similar code in action here: codepad.org/hCLE4ToA.
Also some classes may not appear in get_declared_classes()'s result, because they may not be loaded at the time this function is called. It may be the case if they are autoloaded after you try to instantiate them. See more on autoloading classes here: php.net/autoload.
Does it answer some of your questions? Did it help?