PHP - Extends In Php
I was wondering if there was a class that I could extend that extends the main class in php. For example all of the classes that I am creating will extend the core class or will be extending a class that extends from the core class. However if the class doesnt exist then it will throw an error. What I would like to do is be able to handle this error instead of just throwing the error so I was hoping that there was a class that I could extend the core class from to handle these errors. This way I can use the magic method __get() to handle the error. If there is another way to go about this please let me know so that I can look into that solution.
Similar TutorialsIf I have a base class MyBase: Code: [Select] class MyBase { public $user_id = NULL; public function __construct() { $this->user_id = "justin"; } } Then I have a class which inherits the base class MyClass: Code: [Select] class Test extends MyBase { public static function get_user_id() { echo parent::$user_id; } } Finally calling it: Code: [Select] echo Test::get_user_id(); First question, do I have to create an instance of MyBase, or when Test extends MyBase will it instanciate it automatically and call the MyBase constructor? Second, I am getting the error: PHP Fatal error: Access to undeclared static property: MyBase::$user_id hi phpfreaks I have an class that is called "connDatabase" what I want to do with it is either extend it or implement it. But I don't know which one to use. The other issue I have is say class "a" has three vars that the that the construct needs to obstantiate and "connDatabase" needs three of its own. How do I obstantiate? thanks for any help This code works without problems:
<?php namespace NamespaceA; class A extends \NamespaceB\B {} namespace NamespaceB; class B {} But why the following code cause Fatal error: Class 'NamespaceB\B' not found in ...file? <?php namespace NamespaceA; class A extends \NamespaceB\B {} namespace NamespaceB; class B extends \NamespaceC\C {} namespace NamespaceC; class C {}And this code also works without problems: <?php namespace NamespaceA; class A extends \NamespaceB\B {} namespace NamespaceC; class C {} namespace NamespaceB; class B extends \NamespaceC\C {}Without any namespace, also Fatal error: Class 'B' not found in ...file: <?php class A extends B {} class B extends C {} class C {}Works without problems: <?php class A extends B {} class B {}And yes everything is in the same PHP file.... |