Can not is two includes with same class name

When using php includes of php files…
And Not using autoload magic function
Can use this…
Include A.php class A lib in file index.php
Include a test version (added methods) Atest.php class A lib enhanced… in files called from index.php or iframe inside index.php… can use?

What I can not is two includes with same class name same file and not iframe ?

Is there a special reason why both classes have to have the same name?

The reason is
Are the same just added some methods that share common properties and methods from old…
In future will keep only extended class, the class that has additional methods.

I can Only use one file in index.php and other in an iframe… otherwise (same file) conflict will create error?

Same class… again is to test new code in the same class

Why not do like this?

A.php:

Class A {
    // Construct    
    public function __construct() {
        echo "Calling A construct.<br/>";
    }
    
    // Class A methods
    public function TestA() {
        echo "TestA methdod called.<br/>";
    }
}

ExtentedA.php:

Class ExtentedA extends A {
    
    public function __construct() {
        parent::__construct();
        echo "Calling ExtendtedA construct.<br/>";
    }
    
    // More methods for extended A
    public function TestExtendtedA() {
        echo "ExtendtedATest methdod called.<br/>";
    }
}

then in index.php:

require_once 'A.php';
require_once 'ExtentedA.php';

$a = new ExtentedA();
$a->TestA();    // Call method derived from parent
$a->TestExtendtedA();   // Call method in child class

Output:

Calling A construct.
Calling ExtendtedA construct.
TestA methdod called.
ExtendtedATest methdod called.

Why would there be a reason to make two classes with same name? This way you can just instantiate A class if you need only methods from that class. If you need more methods than A class provides you use the child class.

Cheers,
TeNDoLLA

2 Likes

Even is temporary I should extend class A…? And keep extended for ever?

Without extend approach
If exist iframe can same class be in iframe and same in parent page… ?

Use namespace and alias one. You can have the same class using 2 different namespace, but you can’t have no namespace with the same class names.

index.php

<?php
use \Foo\Foo;
use \Bar\Foo as FooBar;

require('foo.php');
$foo = new Foo();
$foo->run();

require('bar.php');
$bar = new FooBar();
$bar->run();

foo.php

<?php
namespace Foo;

class Foo {

    public function run() {

        print('Just a foo');

    }

}

bar.php

<?php
namespace Bar;

class Foo {

    public function run() {

        print('I am a fun bar');

    }

}

It will output

Just a fooI am a fun bar

With spacing, it’ll output

Just a foo I am a fun bar

Doing

<?php
use \Foo\Foo;
use \Bar\Foo;

require('foo.php');
$foo = new Foo();
$foo->run();

require('bar.php');
$bar = new Foo();
$bar->run();

Will output the results from foo.php for both calls.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.